Daniel Lehman : kernel32: If string arg to FormatMessage is NULL, use "( null)" instead of crashing.

Alexandre Julliard julliard at winehq.org
Mon Jan 9 14:24:30 CST 2012


Module: wine
Branch: master
Commit: b1f04e23bf361e06d239e03693c904a597b9a32d
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=b1f04e23bf361e06d239e03693c904a597b9a32d

Author: Daniel Lehman <dlehman at esri.com>
Date:   Fri Jan  6 10:48:44 2012 -0800

kernel32: If string arg to FormatMessage is NULL, use "(null)" instead of crashing.

---

 dlls/kernel32/format_msg.c       |    9 ++++++---
 dlls/kernel32/tests/format_msg.c |   13 +++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/dlls/kernel32/format_msg.c b/dlls/kernel32/format_msg.c
index d6d19ff..b9e471f 100644
--- a/dlls/kernel32/format_msg.c
+++ b/dlls/kernel32/format_msg.c
@@ -135,15 +135,18 @@ static LPCWSTR format_insert( BOOL unicode_caller, int insert, LPCWSTR format,
     if (*format != '!')  /* simple string */
     {
         arg = get_arg( insert, flags, args );
-        if (unicode_caller)
+        if (unicode_caller || !arg)
         {
-            WCHAR *str = (WCHAR *)arg;
+            static const WCHAR nullW[] = {'(','n','u','l','l',')',0};
+            const WCHAR *str = (const WCHAR *)arg;
+
+            if (!str) str = nullW;
             *result = HeapAlloc( GetProcessHeap(), 0, (strlenW(str) + 1) * sizeof(WCHAR) );
             strcpyW( *result, str );
         }
         else
         {
-            char *str = (char *)arg;
+            const char *str = (const char *)arg;
             DWORD length = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
             *result = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
             MultiByteToWideChar( CP_ACP, 0, str, -1, *result, length );
diff --git a/dlls/kernel32/tests/format_msg.c b/dlls/kernel32/tests/format_msg.c
index 37ea3e9..1f56440 100644
--- a/dlls/kernel32/tests/format_msg.c
+++ b/dlls/kernel32/tests/format_msg.c
@@ -125,6 +125,7 @@ static void test_message_from_string_wide(void)
     static const WCHAR s_sp002sp001[] = {' ',' ','0','0','0','2',',',' ',' ','0','0','1',0};
     static const WCHAR s_sp002sp003[] = {' ',' ','0','0','0','2',',',' ','0','0','0','0','3',0};
     static const WCHAR s_sp001004[]   = {' ',' ','0','0','1',',','0','0','0','0','0','4',0};
+    static const WCHAR s_null[]       = {'(','n','u','l','l',')',0};
 
     static const WCHAR init_buf[] = {'x', 'x', 'x', 'x', 'x', 'x'};
     static const WCHAR broken_buf[] = {'t','e','s','t','x','x'};
@@ -381,6 +382,12 @@ static void test_message_from_string_wide(void)
     ok(!lstrcmpW(s_crlfcrlf, out), "failed out=%s\n", wine_dbgstr_w(out));
     ok(r==4,"failed: r=%d\n", r);
 
+    /* null string as argument */
+    r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_1, 0,
+        0, out, sizeof(out)/sizeof(WCHAR), NULL);
+    ok(!lstrcmpW(s_null, out),"failed out=[%s]\n", wine_dbgstr_w(out));
+    ok(r==6,"failed: r=%d\n",r);
+
     /* precision and width */
 
     r = doitW(FORMAT_MESSAGE_FROM_STRING, fmt_13s,
@@ -700,6 +707,12 @@ static void test_message_from_string(void)
     ok(!strcmp("\r\n\r\n", out),"failed out=[%s]\n",out);
     ok(r==4,"failed: r=%d\n",r);
 
+    /* null string as argument */
+    r = doit(FORMAT_MESSAGE_FROM_STRING, "%1", 0,
+        0, out, sizeof(out)/sizeof(CHAR), NULL);
+    ok(!strcmp("(null)", out),"failed out=[%s]\n",out);
+    ok(r==6,"failed: r=%d\n",r);
+
     /* precision and width */
 
     r = doit(FORMAT_MESSAGE_FROM_STRING, "%1!3s!",




More information about the wine-cvs mailing list