[PATCH 2/3] [Msvcrt]: put in place the full scheme for paramter validation (the _s family of functions)

Eric Pouech eric.pouech at orange.fr
Sun Oct 24 15:07:35 CDT 2010




A+
---

 dlls/msvcrt/data.c   |    7 +++----
 dlls/msvcrt/errno.c  |   14 ++++++++++++++
 dlls/msvcrt/file.c   |    8 ++++----
 dlls/msvcrt/heap.c   |    5 ++---
 dlls/msvcrt/mbcs.c   |    5 ++---
 dlls/msvcrt/msvcrt.h |    4 ++++
 dlls/msvcrt/string.c |   23 +++++++++++------------
 dlls/msvcrt/wcs.c    |   20 +++++++++-----------
 8 files changed, 49 insertions(+), 37 deletions(-)


diff --git a/dlls/msvcrt/data.c b/dlls/msvcrt/data.c
index 068b29f..53dd114 100644
--- a/dlls/msvcrt/data.c
+++ b/dlls/msvcrt/data.c
@@ -228,15 +228,14 @@ MSVCRT_wchar_t*** CDECL __p___winitenv(void) { return &MSVCRT___winitenv; }
 /*********************************************************************
  *		_get_osplatform (MSVCRT.@)
  */
-int CDECL MSVCRT__get_osplatform(int *ret)
+int CDECL MSVCRT__get_osplatform(int *pValue)
 {
-    if(!ret) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(pValue != NULL)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
 
-    *ret = MSVCRT__osplatform;
+    *pValue = MSVCRT__osplatform;
     return 0;
 }
 
diff --git a/dlls/msvcrt/errno.c b/dlls/msvcrt/errno.c
index 266b6db..7e5aa08 100644
--- a/dlls/msvcrt/errno.c
+++ b/dlls/msvcrt/errno.c
@@ -27,6 +27,7 @@
 #include "windef.h"
 #include "winternl.h"
 #include "msvcrt.h"
+#include "winnls.h"
 #include "excpt.h"
 #include "wine/debug.h"
 
@@ -377,6 +378,19 @@ void __cdecl MSVCRT__invalid_parameter(const MSVCRT_wchar_t *expr, const MSVCRT_
     }
 }
 
+void MSVCRT_call_invalid_parameter_handler(const char *expr, const char *func, const char* file,
+                                           unsigned int line, MSVCRT_uintptr_t arg)
+{
+    MSVCRT_wchar_t      exprW[1024];
+    MSVCRT_wchar_t      funcW[256];
+    MSVCRT_wchar_t      fileW[1024];
+
+    MultiByteToWideChar(CP_ACP, 0, expr, -1, exprW, sizeof(exprW) / sizeof(exprW[0]));
+    MultiByteToWideChar(CP_ACP, 0, func, -1, funcW, sizeof(funcW) / sizeof(funcW[0]));
+    MultiByteToWideChar(CP_ACP, 0, file, -1, fileW, sizeof(fileW) / sizeof(fileW[0]));
+    MSVCRT__invalid_parameter(exprW, funcW, fileW, line, arg);
+}
+
 /* _get_invalid_parameter_handler - not exported in native msvcrt, added in msvcr80 */
 MSVCRT_invalid_parameter_handler CDECL _get_invalid_parameter_handler(void)
 {
diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c
index f906401..3eb93ac 100644
--- a/dlls/msvcrt/file.c
+++ b/dlls/msvcrt/file.c
@@ -2686,8 +2686,8 @@ MSVCRT_FILE * CDECL MSVCRT_fopen(const char *path, const char *mode)
 int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
         const char *filename, const char *mode)
 {
-    if(!pFile) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
+        MSVCRT_CHECK_PMT(mode != NULL)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
@@ -2713,8 +2713,8 @@ MSVCRT_FILE * CDECL MSVCRT__wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wcha
 int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
         const MSVCRT_wchar_t *mode)
 {
-    if(!pFile) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
+        MSVCRT_CHECK_PMT(mode != NULL)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
diff --git a/dlls/msvcrt/heap.c b/dlls/msvcrt/heap.c
index b1d4076..beda0bd 100644
--- a/dlls/msvcrt/heap.c
+++ b/dlls/msvcrt/heap.c
@@ -572,8 +572,7 @@ int CDECL strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
     if(!count)
         return 0;
 
-    if(!dest || !src || !numberOfElements) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(dest != NULL) || !MSVCRT_CHECK_PMT(src != NULL) || !MSVCRT_CHECK_PMT(numberOfElements != 0)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
@@ -591,7 +590,7 @@ int CDECL strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
         return 0;
     }
 
-    MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    MSVCRT_INVALID_PMT("numberOfElements too small");
     dest[0] = '\0';
     *MSVCRT__errno() = MSVCRT_EINVAL;
     return MSVCRT_EINVAL;
diff --git a/dlls/msvcrt/mbcs.c b/dlls/msvcrt/mbcs.c
index 76d562c..a84e207 100644
--- a/dlls/msvcrt/mbcs.c
+++ b/dlls/msvcrt/mbcs.c
@@ -1775,8 +1775,7 @@ int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
         return 0;
     }
 
-    if(!mbstr || !wcstr) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if(!MSVCRT_CHECK_PMT(mbstr != NULL) || !MSVCRT_CHECK_PMT(wcstr != NULL)) {
         if(wcstr && size)
             wcstr[0] = '\0';
         *MSVCRT__errno() = MSVCRT_EINVAL;
@@ -1794,7 +1793,7 @@ int CDECL MSVCRT__mbstowcs_s_l(MSVCRT_size_t *ret, MSVCRT_wchar_t *wcstr,
     else if(conv==size && (count==MSVCRT__TRUNCATE || wcstr[conv-1]=='\0'))
         wcstr[conv-1] = '\0';
     else {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+        MSVCRT_INVALID_PMT("wcstr[size] is too small");
         if(size)
             wcstr[0] = '\0';
         *MSVCRT__errno() = MSVCRT_ERANGE;
diff --git a/dlls/msvcrt/msvcrt.h b/dlls/msvcrt/msvcrt.h
index 3d9639e..9630ce5 100644
--- a/dlls/msvcrt/msvcrt.h
+++ b/dlls/msvcrt/msvcrt.h
@@ -846,6 +846,10 @@ void __cdecl    _wsearchenv(const MSVCRT_wchar_t*, const MSVCRT_wchar_t*, MSVCRT
 MSVCRT_intptr_t __cdecl MSVCRT__spawnvpe(int, const char*, const char* const*, const char* const*);
 void __cdecl MSVCRT__invalid_parameter(const MSVCRT_wchar_t *expr, const MSVCRT_wchar_t *func,
                                        const MSVCRT_wchar_t *file, unsigned int line, MSVCRT_uintptr_t arg);
+void MSVCRT_call_invalid_parameter_handler(const char *expr, const char *func,
+                                           const char *file, unsigned int line, MSVCRT_uintptr_t arg);
+#define MSVCRT_INVALID_PMT(x) MSVCRT_call_invalid_parameter_handler(x, __FUNCTION__, __FILE__, __LINE__, 0)
+#define MSVCRT_CHECK_PMT(x) ((!(x)) ? MSVCRT_INVALID_PMT(#x),FALSE : TRUE)
 #endif
 
 #endif /* __WINE_MSVCRT_H */
diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c
index 02bd0a5..d4744f0 100644
--- a/dlls/msvcrt/string.c
+++ b/dlls/msvcrt/string.c
@@ -152,8 +152,8 @@ char * CDECL MSVCRT_strtok( char *str, const char *delim )
  */
 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
 {
-    if(!delim || !ctx || (!str && !*ctx)) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
+        !MSVCRT_CHECK_PMT((str != NULL && *ctx != NULL))) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return NULL;
     }
@@ -206,8 +206,7 @@ double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t loca
     double ret;
     BOOL found_digit = FALSE;
 
-    if(!str) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (str == NULL) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return 0;
     }
@@ -529,8 +528,8 @@ __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCR
 
     TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
 
-    if(!nptr || base<0 || base>36 || base==1) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (nptr == NULL || base<0 || base>36 || base==1) {
+        *MSVCRT__errno() = MSVCRT_EINVAL;
         return 0;
     }
 
@@ -609,8 +608,7 @@ unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int b
 
     TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
 
-    if(!nptr || base<0 || base>36 || base==1) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (nptr == NULL || base<0 || base>36 || base==1) {
         return 0;
     }
 
@@ -681,7 +679,8 @@ int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
     char buffer[33], *pos;
     size_t len;
 
-    if (!str || !size || radix < 2 || radix > 36)
+    if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
+        !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
     {
         if (str && size)
             str[0] = '\0';
@@ -755,8 +754,8 @@ int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
     char buffer[65], *pos;
     int digit;
 
-    if(!str || radix<2 || radix>36) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
+        !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
@@ -775,7 +774,7 @@ int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
     }while(value != 0);
 
     if(buffer-pos+65 > size) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+        MSVCRT_INVALID_PMT("str[size] is too small");
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
diff --git a/dlls/msvcrt/wcs.c b/dlls/msvcrt/wcs.c
index 47a50b6..df5d773 100644
--- a/dlls/msvcrt/wcs.c
+++ b/dlls/msvcrt/wcs.c
@@ -135,8 +135,7 @@ double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
     double ret;
     BOOL found_digit = FALSE;
 
-    if(!str) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (str == NULL) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return 0;
     }
@@ -299,8 +298,7 @@ MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
         return 0;
     }
 
-    if(!wcstr || !mbstr) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (!MSVCRT_CHECK_PMT(wcstr != NULL) || !MSVCRT_CHECK_PMT(mbstr != NULL)) {
         if(mbstr && size)
             mbstr[0] = '\0';
         *MSVCRT__errno() = MSVCRT_EINVAL;
@@ -318,7 +316,7 @@ MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
     else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
         mbstr[conv-1] = '\0';
     else {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+        MSVCRT_INVALID_PMT("mbstr[size] is too small");
         if(size)
             mbstr[0] = '\0';
         *MSVCRT__errno() = MSVCRT_ERANGE;
@@ -1052,7 +1050,7 @@ int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
 
     if(ret<0 || ret==len) {
         if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
-            MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
+            MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
             *MSVCRT__errno() = MSVCRT_ERANGE;
             memset(str, 0, sizeOfBuffer);
         } else
@@ -1190,7 +1188,7 @@ int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer
 
     if(ret<0 || ret==len) {
         if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
-            MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
+            MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
             *MSVCRT__errno() = MSVCRT_ERANGE;
             memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
         } else
@@ -1593,8 +1591,8 @@ __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
 
     TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
 
-    if(!nptr || base<0 || base>36 || base==1) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (nptr == NULL || base < 2 || base > 36) {
+        *MSVCRT__errno() = MSVCRT_EINVAL;
         return 0;
     }
 
@@ -1675,8 +1673,8 @@ unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
 
     TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
 
-    if(!nptr || base<0 || base>36 || base==1) {
-        MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
+    if (nptr == NULL || base<0 || base>36 || base==1) {
+        *MSVCRT__errno() = MSVCRT_EINVAL;
         return 0;
     }
 






More information about the wine-patches mailing list