[PATCH] [Msvcrt]: correctly handle the error cases in strtol and strtoul (fixes #18151)

Eric Pouech eric.pouech at orange.fr
Sun Aug 30 07:17:56 CDT 2009


Try #2. Use this version instead.

A+
---

 dlls/msvcrt/msvcrt.spec    |    4 ++--
 dlls/msvcrt/string.c       |   39 +++++++++++++++++++++++++++++++++++++++
 dlls/msvcrt/tests/string.c |   25 +++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 2 deletions(-)


diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec
index fd03647..5c475a0 100644
--- a/dlls/msvcrt/msvcrt.spec
+++ b/dlls/msvcrt/msvcrt.spec
@@ -767,8 +767,8 @@
 @ cdecl strstr(str str) ntdll.strstr
 @ cdecl strtod(str ptr) MSVCRT_strtod
 @ cdecl strtok(str str) MSVCRT_strtok
-@ cdecl strtol(str ptr long) ntdll.strtol
-@ cdecl strtoul(str ptr long) ntdll.strtoul
+@ cdecl strtol(str ptr long) MSVCRT_strtol
+@ cdecl strtoul(str ptr long) MSVCRT_strtoul
 @ cdecl strxfrm(ptr str long) MSVCRT_strxfrm
 @ varargs swprintf(ptr wstr) MSVCRT_swprintf
 @ varargs swscanf(wstr wstr) MSVCRT_swscanf
diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c
index 74beefb..0e04ef8 100644
--- a/dlls/msvcrt/string.c
+++ b/dlls/msvcrt/string.c
@@ -25,6 +25,7 @@
 #include "config.h"
 
 #include <stdlib.h>
+#include <errno.h>
 #include "msvcrt.h"
 #include "wine/debug.h"
 
@@ -250,3 +251,41 @@ int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str,
 #endif
     return 0;
 }
+
+/******************************************************************
+ *		strtol (MSVCRT.@)
+ */
+long int MSVCRT_strtol(const char* nptr, char** end, int base)
+{
+    /* wrapper to forward libc error code to msvcrt's error codes */
+    long ret;
+
+    errno = 0;
+    ret = strtol(nptr, end, base);
+    switch (errno)
+    {
+    case ERANGE:        *MSVCRT__errno() = MSVCRT_ERANGE;       break;
+    case EINVAL:        *MSVCRT__errno() = MSVCRT_EINVAL;       break;
+    }
+
+    return ret;
+}
+
+/******************************************************************
+ *		strtoul (MSVCRT.@)
+ */
+unsigned long int MSVCRT_strtoul(const char* nptr, char** end, int base)
+{
+    /* wrapper to forward libc error code to msvcrt's error codes */
+    unsigned long ret;
+
+    errno = 0;
+    ret = strtoul(nptr, end, base);
+    switch (errno)
+    {
+    case ERANGE:        *MSVCRT__errno() = MSVCRT_ERANGE;       break;
+    case EINVAL:        *MSVCRT__errno() = MSVCRT_EINVAL;       break;
+    }
+
+    return ret;
+}
diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c
index 96bcb7f..14c0189 100644
--- a/dlls/msvcrt/tests/string.c
+++ b/dlls/msvcrt/tests/string.c
@@ -663,6 +663,30 @@ static void test_strtok(void)
     }
 }
 
+static void test_strtol(void)
+{
+    char* e;
+    long l;
+    unsigned long ul;
+
+    errno = EBADF;
+    l = strtol("-1234", &e, 0);
+    ok(l==-1234, "wrong value %ld\n", l);
+    ok(errno == EBADF, "wrong errno %d\n", errno);
+    errno = EBADF;
+    ul = strtoul("1234", &e, 0);
+    ok(ul==1234, "wrong value %lu\n", ul);
+    ok(errno == EBADF, "wrong errno %d\n", errno);
+    errno = 0;
+    l = strtol("9223372036854775807L", &e, 0);
+    ok(l==2147483647, "wrong value %ld\n", l);
+    ok(errno == ERANGE, "wrong errno %d\n", errno);
+    errno = 0;
+    ul = strtoul("9223372036854775807L", &e, 0);
+    ok(ul==4294967295ul, "wrong value %lu\n", ul);
+    ok(errno == ERANGE, "wrong errno %d\n", errno);
+}
+
 START_TEST(string)
 {
     char mem[100];
@@ -706,4 +730,5 @@ START_TEST(string)
     test_mbcjisjms();
     test_strtok();
     test_wcscpy_s();
+    test_strtol();
 }






More information about the wine-patches mailing list