crt: Set errno where needed in __mingw_wcstod

This is similar to faa833310419c49cfaeeecb53f9d7366cdc1f120 but for
the __mingw_wcstod function which is implemented around __mingw_wcstold.

Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-crt/misc/mingw_wcstod.c b/mingw-w64-crt/misc/mingw_wcstod.c
index 394c0f1..e35a952 100644
--- a/mingw-w64-crt/misc/mingw_wcstod.c
+++ b/mingw-w64-crt/misc/mingw_wcstod.c
@@ -1,4 +1,7 @@
 #include <stdio.h>
+#include <float.h>
+#include <errno.h>
+#include <math.h>
 
 extern long double __cdecl
 __mingw_wcstold (const wchar_t * __restrict__ _Str, wchar_t ** __restrict__ _EndPtr);
@@ -9,7 +12,20 @@
 double __cdecl
 __mingw_wcstod (const wchar_t * __restrict__ _Str, wchar_t ** __restrict__ _EndPtr)
 {
-  return (double) __mingw_wcstold (_Str, _EndPtr);
+  long double ret = __mingw_wcstold (_Str, _EndPtr);
+  if (isfinite(ret)) {
+    /* Check for cases that aren't out of range for long doubles, but that are
+     * for doubles. */
+    if (ret > DBL_MAX)
+      errno = ERANGE;
+    else if (ret < -DBL_MAX)
+      errno = ERANGE;
+    else if (ret > 0 && ret < DBL_MIN)
+      errno = ERANGE;
+    else if (ret < 0 && ret > -DBL_MIN)
+      errno = ERANGE;
+  }
+  return ret;
 }