Eric Pouech : winedbg: Do proper error management when reallocating memory.

Alexandre Julliard julliard at winehq.org
Mon Jul 25 16:18:23 CDT 2022


Module: wine
Branch: master
Commit: 674ef559e0e1ce995fe00fe08ee25cd9f3eb2b49
URL:    https://gitlab.winehq.org/wine/wine/-/commit/674ef559e0e1ce995fe00fe08ee25cd9f3eb2b49

Author: Eric Pouech <eric.pouech at gmail.com>
Date:   Wed Jul 20 14:28:42 2022 +0200

winedbg: Do proper error management when reallocating memory.

Signed-off-by: Eric Pouech <eric.pouech at gmail.com>

---

 programs/winedbg/break.c   | 34 ++++++++++++++++++++--------------
 programs/winedbg/dbg.y     |  8 +++++++-
 programs/winedbg/display.c |  7 +++++--
 programs/winedbg/info.c    | 19 +++++++++++++++----
 programs/winedbg/stack.c   |  6 ++++--
 5 files changed, 51 insertions(+), 23 deletions(-)

diff --git a/programs/winedbg/break.c b/programs/winedbg/break.c
index 27a11a00d33..90df7d1e8b5 100644
--- a/programs/winedbg/break.c
+++ b/programs/winedbg/break.c
@@ -202,6 +202,7 @@ BOOL break_add_break(const ADDRESS64* addr, BOOL verbose, BOOL swbp)
  */
 BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
 {
+    struct dbg_delayed_bp* new;
     ADDRESS64   addr;
 
     types_extract_as_address(lvalue, &addr);
@@ -215,13 +216,15 @@ BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
             return FALSE;
         }
         dbg_printf("Unable to add breakpoint, will check again any time a new DLL is loaded\n");
-        dbg_curr_process->delayed_bp = 
-            dbg_heap_realloc(dbg_curr_process->delayed_bp,
-                             sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
-
-        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol   = FALSE;
-        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp = swbp;
-        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.addr      = addr;
+        new = dbg_heap_realloc(dbg_curr_process->delayed_bp,
+                               sizeof(struct dbg_delayed_bp) * (dbg_curr_process->num_delayed_bp + 1));
+        if (!new) return FALSE;
+        dbg_curr_process->delayed_bp = new;
+
+        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].is_symbol   = FALSE;
+        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].software_bp = swbp;
+        dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].u.addr      = addr;
+        dbg_curr_process->num_delayed_bp++;
         return TRUE;
     }
     return FALSE;
@@ -234,6 +237,7 @@ BOOL break_add_break_from_lvalue(const struct dbg_lvalue* lvalue, BOOL swbp)
  */
 void	break_add_break_from_id(const char *name, int lineno, BOOL swbp)
 {
+    struct dbg_delayed_bp* new;
     struct dbg_lvalue 	lvalue;
     int		        i;
 
@@ -256,13 +260,15 @@ void	break_add_break_from_id(const char *name, int lineno, BOOL swbp)
             lineno == dbg_curr_process->delayed_bp[i].u.symbol.lineno)
             return;
     }
-    dbg_curr_process->delayed_bp = dbg_heap_realloc(dbg_curr_process->delayed_bp,
-                                                    sizeof(struct dbg_delayed_bp) * ++dbg_curr_process->num_delayed_bp);
-
-    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].is_symbol       = TRUE;
-    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].software_bp     = swbp;
-    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.name   = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1), name);
-    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp - 1].u.symbol.lineno = lineno;
+    new = dbg_heap_realloc(dbg_curr_process->delayed_bp,
+                           sizeof(struct dbg_delayed_bp) * (dbg_curr_process->num_delayed_bp + 1));
+    if (!new) return;
+    dbg_curr_process->delayed_bp = new;
+    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].is_symbol       = TRUE;
+    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].software_bp     = swbp;
+    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].u.symbol.name   = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1), name);
+    dbg_curr_process->delayed_bp[dbg_curr_process->num_delayed_bp].u.symbol.lineno = lineno;
+    dbg_curr_process->num_delayed_bp++;
 }
 
 struct cb_break_lineno
diff --git a/programs/winedbg/dbg.y b/programs/winedbg/dbg.y
index 30701a660be..3a5392596de 100644
--- a/programs/winedbg/dbg.y
+++ b/programs/winedbg/dbg.y
@@ -514,8 +514,14 @@ static int input_fetch_entire_line(const char* pfx, char** line)
 
         if (len + 2 > alloc)
         {
+            char* new;
             while (len + 2 > alloc) alloc *= 2;
-            buffer = dbg_heap_realloc(buffer, alloc);
+            if (!(new = dbg_heap_realloc(buffer, alloc)))
+            {
+                HeapFree(GetProcessHeap(), 0, buffer);
+                return -1;
+            }
+            buffer = new;
         }
         buffer[len++] = ch;
     }
diff --git a/programs/winedbg/display.c b/programs/winedbg/display.c
index 11de3c12504..f38b3df206a 100644
--- a/programs/winedbg/display.c
+++ b/programs/winedbg/display.c
@@ -63,10 +63,13 @@ BOOL display_add(struct expr *exp, int count, char format)
 
     if (i == maxdisplays)
     {
+        struct display *new;
 	/* no space left - expand */
+        new = dbg_heap_realloc(displaypoints,
+                               (maxdisplays + DISPTAB_DELTA) * sizeof(*displaypoints));
+        if (!new) return FALSE;
+        displaypoints = new;
         maxdisplays += DISPTAB_DELTA;
-        displaypoints = dbg_heap_realloc(displaypoints,
-                                         maxdisplays * sizeof(*displaypoints));
     }
 
     if (i == ndisplays) ndisplays++;
diff --git a/programs/winedbg/info.c b/programs/winedbg/info.c
index ae0fd90e938..2ba7c5950d0 100644
--- a/programs/winedbg/info.c
+++ b/programs/winedbg/info.c
@@ -199,8 +199,10 @@ static BOOL CALLBACK info_mod_cb(PCSTR mod_name, DWORD64 base, PVOID ctx)
 
     if (im->num_used + 1 > im->num_alloc)
     {
+        struct info_module* new = dbg_heap_realloc(im->modules, (im->num_alloc + 16) * sizeof(*im->modules));
+        if (!new) return FALSE; /* stop enumeration in case of OOM */
         im->num_alloc += 16;
-        im->modules = dbg_heap_realloc(im->modules, im->num_alloc * sizeof(*im->modules));
+        im->modules = new;
     }
     im->modules[im->num_used].mi.SizeOfStruct = sizeof(im->modules[im->num_used].mi);
     if (SymGetModuleInfo64(dbg_curr_process->handle, base, &im->modules[im->num_used].mi))
@@ -315,8 +317,10 @@ static void class_walker(HWND hWnd, struct class_walker* cw)
     {
         if (cw->used >= cw->alloc)
         {
+            ATOM* new = dbg_heap_realloc(cw->table, (cw->alloc + 16) * sizeof(ATOM));
+            if (!new) return;
             cw->alloc += 16;
-            cw->table = dbg_heap_realloc(cw->table, cw->alloc * sizeof(ATOM));
+            cw->table = new;
         }
         cw->table[cw->used++] = atom;
         info_win32_class(hWnd, clsName);
@@ -547,8 +551,15 @@ void info_win32_processes(void)
             dp.entries[dp.count++].children = -1;
             if (dp.count >= dp.alloc)
             {
-                dp.entries = HeapReAlloc(GetProcessHeap(), 0, dp.entries, sizeof(*dp.entries) * (dp.alloc *= 2));
-                if (!dp.entries) return;
+                struct dump_proc_entry* new = HeapReAlloc(GetProcessHeap(), 0, dp.entries, sizeof(*dp.entries) * (dp.alloc * 2));
+                if (!new)
+                {
+                    CloseHandle(snap);
+                    HeapFree(GetProcessHeap(), 0, dp.entries);
+                    return;
+                }
+                dp.alloc *= 2;
+                dp.entries = new;
             }
             dp.entries[dp.count].proc.dwSize = sizeof(dp.entries[dp.count].proc);
             ok = Process32Next(snap, &dp.entries[dp.count].proc);
diff --git a/programs/winedbg/stack.c b/programs/winedbg/stack.c
index 01a86108d6e..25328a867e2 100644
--- a/programs/winedbg/stack.c
+++ b/programs/winedbg/stack.c
@@ -204,8 +204,10 @@ unsigned stack_fetch_frames(const dbg_ctx_t* _ctx)
                               SymFunctionTableAccess64, SymGetModuleBase64, NULL, SYM_STKWALK_DEFAULT)) ||
            nf == 0) /* we always register first frame information */
     {
-        dbg_curr_thread->frames = dbg_heap_realloc(dbg_curr_thread->frames,
-                                                   (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
+        struct dbg_frame* new = dbg_heap_realloc(dbg_curr_thread->frames,
+                                                 (nf + 1) * sizeof(dbg_curr_thread->frames[0]));
+        if (!new) break;
+        dbg_curr_thread->frames = new;
 
         dbg_curr_thread->frames[nf].addr_pc      = sf.AddrPC;
         dbg_curr_thread->frames[nf].linear_pc    = (DWORD_PTR)memory_to_linear_addr(&sf.AddrPC);




More information about the wine-cvs mailing list