[PATCH] overflow checking for MultiByteToWideChar/WideCharToMultiByte

Marcus Meissner marcus at jet.franken.de
Sun Dec 3 09:24:36 CST 2006


Hi,

This is more a FYI patch and GCC 4.0+ specific.

This enhancement detects if the size of a known buffer is different than
the specified size.

Thie tests/sock.c adjustement is necessary due to stdlib.h inclusion.

Ciao, Marcus
---

 dlls/kernel32/locale.c   |    2 ++
 dlls/ws2_32/tests/sock.c |    2 +-
 include/winnls.h         |   45 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletions(-)

d0e7840335b6bbe4990b1c043dcff9b298c7a5a8
diff --git a/dlls/kernel32/locale.c b/dlls/kernel32/locale.c
index bc7db75..1d4ceaf 100644
--- a/dlls/kernel32/locale.c
+++ b/dlls/kernel32/locale.c
@@ -1757,6 +1757,7 @@ BOOL WINAPI EnumSystemCodePagesW( CODEPA
  *            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 )
 {
@@ -1850,6 +1851,7 @@ INT WINAPI MultiByteToWideChar( UINT pag
  *            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/dlls/ws2_32/tests/sock.c b/dlls/ws2_32/tests/sock.c
index c8b9cc8..f5ed0c1 100644
--- a/dlls/ws2_32/tests/sock.c
+++ b/dlls/ws2_32/tests/sock.c
@@ -22,8 +22,8 @@
 #include <stdarg.h>
 
 #include <windef.h>
-#include <winbase.h>
 #include <winsock2.h>
+#include <winbase.h>
 #include <mswsock.h>
 #include "wine/test.h"
 #include <winnt.h>
diff --git a/include/winnls.h b/include/winnls.h
index 8888171..c13c0bf 100644
--- a/include/winnls.h
+++ b/include/winnls.h
@@ -20,6 +20,10 @@
 #define __WINE_WINNLS_H
 #ifndef NONLS
 
+#ifndef RC_INVOKED
+# include <stdlib.h>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -768,6 +772,26 @@ INT         WINAPI LCMapStringW(LCID,DWO
 #define     LCMapString WINELIB_NAME_AW(LCMapString)
 LCID        WINAPI LocaleNameToLCID(LPCWSTR,DWORD);
 INT         WINAPI MultiByteToWideChar(UINT,DWORD,LPCSTR,INT,LPWSTR,INT);
+
+static inline INT
+WINAPI MultiByteToWideChar_ichk(
+	UINT cp,DWORD flags,
+	LPCSTR src,INT srclen,INT srcbuflen,
+	LPWSTR dst,INT dstlen,INT dstbuflen
+) {
+	if ((srclen != -1) && (srcbuflen != -1) && (srcbuflen < srclen))
+		abort();
+	if ((dstlen != -1)  && (dstbuflen != -1) && (dstbuflen/2 < dstlen))
+		abort();
+	/* later add assertions regarding conversion lengths */
+	return MultiByteToWideChar(cp,flags,src,srclen,dst,dstlen);
+}
+#define MultiByteToWideChar(cp,flags,src,srclen,dst,dstlen)	\
+	MultiByteToWideChar_ichk(cp,flags,			\
+		src,srclen,__builtin_object_size(src,0),	\
+		dst,dstlen,__builtin_object_size(dst,0)		\
+	)
+
 INT         WINAPI SetCalendarInfoA(LCID,CALID,CALTYPE,LPCSTR);
 INT         WINAPI SetCalendarInfoW(LCID,CALID,CALTYPE,LPCWSTR);
 #define     SetCalendarInfo WINELIB_NAME_AW(SetCalendarInfo)
@@ -778,6 +802,27 @@ BOOL        WINAPI SetThreadLocale(LCID)
 BOOL        WINAPI SetUserGeoID(GEOID);
 INT         WINAPI WideCharToMultiByte(UINT,DWORD,LPCWSTR,INT,LPSTR,INT,LPCSTR,LPBOOL);
 
+static inline INT
+WINAPI WideCharToMultiByte_ichk(
+	UINT cp,DWORD flags,
+	LPCWSTR src,INT srclen,INT srcbuflen,
+	LPSTR dst,INT dstlen,INT dstbuflen,
+	LPCSTR xstr,LPBOOL b
+) {
+	if ((srclen != -1) && (srcbuflen != -1) && (srcbuflen/2 < srclen))
+		abort();
+	if ((dstlen != -1)  && (dstbuflen != -1) && (dstbuflen < dstlen))
+		abort();
+	/* later add assertions regarding conversion lengths */
+	return WideCharToMultiByte(cp,flags,src,srclen,dst,dstlen,xstr,b);
+}
+#define WideCharToMultiByte(cp,flags,src,srclen,dst,dstlen,defchar,b)	\
+	WideCharToMultiByte_ichk(cp,flags,				\
+		src,srclen,__builtin_object_size(src,0),		\
+		dst,dstlen,__builtin_object_size(dst,0),		\
+		defchar,b							\
+	)
+
 #ifdef __cplusplus
 }
 #endif
-- 
1.2.4



More information about the wine-devel mailing list