winecfg: Add missing checking of HeapAlloc return values.

Grazvydas Ignotas notasas at gmail.com
Thu Jul 21 16:44:55 CDT 2011


---
 programs/winecfg/winecfg.c |   18 +++++++++++++-----
 programs/winecfg/winecfg.h |   14 ++++++++++----
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/programs/winecfg/winecfg.c b/programs/winecfg/winecfg.c
index 1d2ec50..fb2624e 100644
--- a/programs/winecfg/winecfg.c
+++ b/programs/winecfg/winecfg.c
@@ -87,8 +87,11 @@ WCHAR* load_string (UINT id)
 
     len = lstrlenW (buf);
     newStr = HeapAlloc (GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR));
-    memcpy (newStr, buf, len * sizeof (WCHAR));
-    newStr[len] = 0;
+    if (newStr)
+    {
+        memcpy (newStr, buf, len * sizeof (WCHAR));
+        newStr[len] = 0;
+    }
     return newStr;
 }
 
@@ -368,7 +371,7 @@ static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, cons
                 break;
             case REG_DWORD:
                 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
-                memcpy( s->value, value, sizeof(DWORD) );
+                if (s->value) memcpy( s->value, value, sizeof(DWORD) );
                 break;
         }
 
@@ -387,6 +390,7 @@ static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, cons
 
     /* otherwise add a new setting for it  */
     s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
+    if (!s) return;
     s->root  = root;
     s->path  = strdupW(path);
     s->name  = name  ? strdupW(name)  : NULL;
@@ -398,7 +402,7 @@ static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, cons
             break;
         case REG_DWORD:
             s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
-            memcpy( s->value, value, sizeof(DWORD) );
+            if (s->value) memcpy( s->value, value, sizeof(DWORD) );
             break;
     }
 
@@ -583,6 +587,7 @@ char **enumerate_values(HKEY root, char *path)
     {
         for(len=0; wret[len]; len++);
         ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(char*));
+        if (!ret) goto end;
 
         /* convert WCHAR ** to char ** and HeapFree each WCHAR * element on our way */
         for (i=0; i<len; i++)
@@ -596,6 +601,7 @@ char **enumerate_values(HKEY root, char *path)
         ret[len] = NULL;
     }
 
+end:
     HeapFree(GetProcessHeap(), 0, wpath);
     HeapFree(GetProcessHeap(), 0, wret);
 
@@ -689,6 +695,7 @@ char *keypath(const char *section)
     if (current_app)
     {
         result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + lstrlenW(current_app)*2 + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
+        if (!result) return NULL;
         wsprintf(result, "AppDefaults\\%ls", current_app);
         if (section[0]) sprintf( result + strlen(result), "\\%s", section );
     }
@@ -711,6 +718,7 @@ WCHAR *keypathW(const WCHAR *section)
     {
         DWORD len = sizeof(appdefaultsW) + (lstrlenW(current_app) + lstrlenW(section) + 1) * sizeof(WCHAR);
         result = HeapAlloc(GetProcessHeap(), 0, len );
+        if (!result) return NULL;
         lstrcpyW( result, appdefaultsW );
         lstrcatW( result, current_app );
         if (section[0])
@@ -756,7 +764,7 @@ int initialize(HINSTANCE hInstance)
 
     /* we could probably just have the list as static data  */
     settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
-    list_init(settings);
+    if (settings) list_init(settings);
 
     return 0;
 }
diff --git a/programs/winecfg/winecfg.h b/programs/winecfg/winecfg.h
index 1959409..761aba6 100644
--- a/programs/winecfg/winecfg.h
+++ b/programs/winecfg/winecfg.h
@@ -126,14 +126,20 @@ void PRINTERROR(void); /* WINE_TRACE() the plaintext error message from GetLastE
 /* returns a string in the win32 heap  */
 static inline char *strdupA(const char *s)
 {
-    char *r = HeapAlloc(GetProcessHeap(), 0, strlen(s)+1);
-    return strcpy(r, s);
+    char *r;
+    int len = strlen(s) + 1;
+    r = HeapAlloc(GetProcessHeap(), 0, len);
+    if (r) memcpy(r, s, len);
+    return r;
 }
 
 static inline WCHAR *strdupW(const WCHAR *s)
 {
-    WCHAR *r = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(s)+1)*sizeof(WCHAR));
-    return lstrcpyW(r, s);
+    WCHAR *r;
+    int len = (lstrlenW(s) + 1) * sizeof(WCHAR);
+    r = HeapAlloc(GetProcessHeap(), 0, len);
+    if (r) memcpy(r, s, len);
+    return r;
 }
 
 /* create a unicode string from a string in Unix locale */
-- 
1.7.0.4




More information about the wine-patches mailing list