Hello Detlef,

although Smatch could have handled the task too of finding places where Wine passes -1 as destlen to MultiByteToWideChar / WideChatToMultiByte this can be done with the C compiler too. Please see the attached patch.

This solution doesn't finds more occurrences that you have found and patched already. But this is really only the stuff that the compiler can figure out at compile time. It has no chance at all to catch stuff like

void foo(int bar)
{
    int dstlen = -1;

    if (bar)
        dstlen = 10;

    MultiByteToWideChar(a, b, c, d, e, dstlen)
}

Those can't be found by a grep either and I think that's why Alexandre is reluctant to apply your patches.

bye
        michael
diff --git a/dlls/kernel32/locale.c b/dlls/kernel32/locale.c
index 9071ff9..f61c4fe 100644
--- a/dlls/kernel32/locale.c
+++ b/dlls/kernel32/locale.c
@@ -1796,6 +1796,7 @@ BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD fla
  *            is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
  *            possible for src.
  */
+#undef MultiByteToWideChar
 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
                                 LPWSTR dst, INT dstlen )
 {
@@ -1887,6 +1888,7 @@ INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
  *            and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
  *            parameter was given.
  */
+#undef WideCharToMultiByte
 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
 {
diff --git a/include/winnls.h b/include/winnls.h
index 50d6389..5c1c269 100644
--- a/include/winnls.h
+++ b/include/winnls.h
@@ -788,6 +788,18 @@ WINBASEAPI BOOL        WINAPI SetThreadLocale(LCID);
 WINBASEAPI BOOL        WINAPI SetUserGeoID(GEOID);
 WINBASEAPI INT         WINAPI WideCharToMultiByte(UINT,DWORD,LPCWSTR,INT,LPSTR,INT,LPCSTR,LPBOOL);
 
+/*
+ * Produce a compile error if we pass a dstlen < 0 to this functions.
+ * BUILD_BUG_ON() shamelessly ripped from the Linux Kernel.
+ */
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define MultiByteToWideChar(a, b, c, d, e, dstlen) \
+    ( BUILD_BUG_ON((dstlen) < 0), \
+    MultiByteToWideChar(a, b, c, d, e, dstlen) )
+#define WideCharToMultiByte(a, b, c, d, e, dstlen, f, g) \
+    ( BUILD_BUG_ON((dstlen) < 0), \
+    WideCharToMultiByte(a, b, c, d, e, dstlen, f, g) )
+
 #ifdef __cplusplus
 }
 #endif


Reply via email to