]> git.ipfire.org Git - thirdparty/newt.git/commitdiff
improve handling of malloc failures master
authorRobert Gill <rtgill82@gmail.com>
Wed, 18 Jun 2025 20:54:37 +0000 (20:54 +0000)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 19 Jun 2025 06:48:28 +0000 (08:48 +0200)
Return NULL on malloc failures when creating new components and from
newtReflowText. Internal buffers are still not checked, but this should
allow some memory allocation errors to be made more obvious and possibly
caught without breaking current API compatibility.

[ML: fixed fields deallocation in newtCreateGrid()]

buttonbar.c
checkbox.c
checkboxtree.c
entry.c
form.c
grid.c
label.c
scale.c
scrollbar.c
textbox.c

index cf48609a50a42b0afdc06d6a7cc139e7119f2fe2..e602acd3b41281cc4c9c322adc5b6a3065dae577 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdarg.h>
 #include <stdarg.h>
+#include <stddef.h>
 
 #include "newt.h"
 
 
 #include "newt.h"
 
@@ -21,6 +22,8 @@ newtGrid newtButtonBarv(char * button1, newtComponent * b1comp, va_list args) {
     }
 
     grid = newtCreateGrid(num, 1);
     }
 
     grid = newtCreateGrid(num, 1);
+    if (grid == NULL)
+       return NULL;
 
     for (i = 0; i < num; i++) {
        *buttons[i].compPtr = newtButton(-1, -1, buttons[i].name);
 
     for (i = 0; i < num; i++) {
        *buttons[i].compPtr = newtButton(-1, -1, buttons[i].name);
index 159076240eacebc78a2a144b7717f13303f9353e..0130d3dad449e1f1dec4dc553b034d6542a34f48 100644 (file)
@@ -45,6 +45,8 @@ newtComponent newtRadiobutton(int left, int top, const char * text, int isDefaul
        initialValue = ' ';
 
     co = newtCheckbox(left, top, text, initialValue, " *", NULL);
        initialValue = ' ';
 
     co = newtCheckbox(left, top, text, initialValue, " *", NULL);
+    if (co == NULL)
+       return NULL;
     rb = co->data;
     rb->type = RADIO;
 
     rb = co->data;
     rb->type = RADIO;
 
index 95156ebd2cce4dd28775f85d970046e6766c3608..f9c2f3e4d0a7dce73626ba22aedbe1d2a67f6455 100644 (file)
@@ -332,7 +332,13 @@ newtComponent newtCheckboxTreeMulti(int left, int top, int height, char *seq, in
     struct CheckboxTree * ct;
 
     co = malloc(sizeof(*co));
     struct CheckboxTree * ct;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
     ct = malloc(sizeof(struct CheckboxTree));
     ct = malloc(sizeof(struct CheckboxTree));
+    if (ct == NULL) {
+       free(co);
+       return NULL;
+    }
     co->callback = NULL;
     co->destroyCallback = NULL;
     co->data = ct;
     co->callback = NULL;
     co->destroyCallback = NULL;
     co->data = ct;
diff --git a/entry.c b/entry.c
index 8dad8c8d5bf10c98582c9c0fc1a9c59d2cb4d29d..815be523d828c8c9c9af9a9db8160c9c0bca55b2 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -71,7 +71,13 @@ newtComponent newtEntry(int left, int top, const char * initialValue, int width,
     struct entry * en;
 
     co = malloc(sizeof(*co));
     struct entry * en;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
     en = malloc(sizeof(struct entry));
     en = malloc(sizeof(struct entry));
+    if (en == NULL) {
+       free(co);
+       return NULL;
+    }
     co->data = en;
 
     co->top = top;
     co->data = en;
 
     co->top = top;
@@ -100,6 +106,11 @@ newtComponent newtEntry(int left, int top, const char * initialValue, int width,
        en->bufAlloced = strlen(initialValue) + 1;
     }
     en->buf = malloc(en->bufAlloced);
        en->bufAlloced = strlen(initialValue) + 1;
     }
     en->buf = malloc(en->bufAlloced);
+    if (en->buf == NULL) {
+       free(en);
+       free(co);
+       return NULL;
+    }
     en->resultPtr = resultPtr;
     if (en->resultPtr) *en->resultPtr = en->buf;
 
     en->resultPtr = resultPtr;
     if (en->resultPtr) *en->resultPtr = en->buf;
 
diff --git a/form.c b/form.c
index 24c601d41f3772f8c02e4ef04facf0f1431baac7..ad425af6c5b06b16fa4cf8826bddb0972e883804 100644 (file)
--- a/form.c
+++ b/form.c
@@ -445,7 +445,15 @@ newtComponent newtForm(newtComponent vertBar, void * help, int flags) {
     struct form * form;
 
     co = malloc(sizeof(*co));
     struct form * form;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
+
     form = malloc(sizeof(*form));
     form = malloc(sizeof(*form));
+    if (form == NULL) {
+       free(co);
+       return NULL;
+    }
+
     co->data = form;
     co->width = 0;
     co->height = 0;
     co->data = form;
     co->width = 0;
     co->height = 0;
@@ -458,6 +466,9 @@ newtComponent newtForm(newtComponent vertBar, void * help, int flags) {
     co->callback = NULL;
     co->destroyCallback = NULL;
 
     co->callback = NULL;
     co->destroyCallback = NULL;
 
+    form->elements = NULL;
+    form->hotKeys = NULL;
+
     form->help = help;
     form->flags = flags;
     form->numCompsAlloced = 5;
     form->help = help;
     form->flags = flags;
     form->numCompsAlloced = 5;
@@ -470,9 +481,12 @@ newtComponent newtForm(newtComponent vertBar, void * help, int flags) {
     form->maxFd = 0;
     form->fds = NULL;
     form->elements = malloc(sizeof(*(form->elements)) * form->numCompsAlloced);
     form->maxFd = 0;
     form->fds = NULL;
     form->elements = malloc(sizeof(*(form->elements)) * form->numCompsAlloced);
+    if (form->elements == NULL) goto error;
 
     form->background = COLORSET_WINDOW;
     form->hotKeys = malloc(sizeof(int));
 
     form->background = COLORSET_WINDOW;
     form->hotKeys = malloc(sizeof(int));
+    if (form->hotKeys == NULL) goto error;
+
     form->numHotKeys = 0;
     form->timer = 0;
     form->lastTimeout.tv_sec = form->lastTimeout.tv_usec = 0;
     form->numHotKeys = 0;
     form->timer = 0;
     form->lastTimeout.tv_sec = form->lastTimeout.tv_usec = 0;
@@ -489,6 +503,14 @@ newtComponent newtForm(newtComponent vertBar, void * help, int flags) {
     form->helpCb = helpCallback;
 
     return co;
     form->helpCb = helpCallback;
 
     return co;
+
+error:
+    free(form->hotKeys);
+    free(form->elements);
+    free(form);
+    free(co);
+
+    return NULL;
 }
 
 newtComponent newtFormGetCurrent(newtComponent co) {
 }
 
 newtComponent newtFormGetCurrent(newtComponent co) {
diff --git a/grid.c b/grid.c
index 6f79ce46ada78f9123b52fc987cabe6c818602bf..2c193d1aa7f8964fe26b0e12450f670b90506d24 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -33,12 +33,26 @@ newtGrid newtCreateGrid(int cols, int rows) {
     newtGrid grid;
 
     grid = malloc(sizeof(*grid));
     newtGrid grid;
 
     grid = malloc(sizeof(*grid));
+    if (grid == NULL)
+       return NULL;
     grid->rows = rows;
     grid->cols = cols;
 
     grid->fields = malloc(sizeof(*grid->fields) * cols);
     grid->rows = rows;
     grid->cols = cols;
 
     grid->fields = malloc(sizeof(*grid->fields) * cols);
+    if (grid->fields == NULL) {
+       free(grid);
+       return NULL;
+    }
+
     while (cols--) {
        grid->fields[cols] = malloc(sizeof(**(grid->fields)) * rows);
     while (cols--) {
        grid->fields[cols] = malloc(sizeof(**(grid->fields)) * rows);
+       if (grid->fields[cols] == NULL) {
+           for (cols++; cols < grid->cols; cols++)
+               free(grid->fields[cols]);
+           free(grid->fields);
+           free(grid);
+           return NULL;
+       }
        memset(grid->fields[cols], 0, sizeof(**(grid->fields)) * rows);
     }
 
        memset(grid->fields[cols], 0, sizeof(**(grid->fields)) * rows);
     }
 
@@ -299,6 +313,8 @@ static newtGrid stackem(int isVert, enum newtGridElement type1, void * what1,
     }
 
     grid = newtCreateGrid(isVert ? 1 : num, isVert ? num : 1);
     }
 
     grid = newtCreateGrid(isVert ? 1 : num, isVert ? num : 1);
+    if (grid == NULL)
+       return NULL;
 
     for (i = 0; i < num; i++) {
        newtGridSetField(grid, isVert ? 0 : i, isVert ? i : 0, 
 
     for (i = 0; i < num; i++) {
        newtGridSetField(grid, isVert ? 0 : i, isVert ? i : 0, 
@@ -367,6 +383,9 @@ newtGrid newtGridBasicWindow(newtComponent text, newtGrid middle,
     newtGrid grid;
 
     grid = newtCreateGrid(1, 3);
     newtGrid grid;
 
     grid = newtCreateGrid(1, 3);
+    if (grid == NULL)
+       return NULL;
+
     newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
                     0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
     newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, middle,
     newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
                     0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
     newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, middle,
@@ -382,6 +401,9 @@ newtGrid newtGridSimpleWindow(newtComponent text, newtComponent middle,
     newtGrid grid;
 
     grid = newtCreateGrid(1, 3);
     newtGrid grid;
 
     grid = newtCreateGrid(1, 3);
+    if (grid == NULL)
+       return NULL;
+
     newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
                     0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
     newtGridSetField(grid, 0, 1, NEWT_GRID_COMPONENT, middle,
     newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
                     0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0);
     newtGridSetField(grid, 0, 1, NEWT_GRID_COMPONENT, middle,
diff --git a/label.c b/label.c
index 85fa518a99f157dda0e820ee01356bd332b7d7f8..1c58480e57ad6decdd41da2d26a1b067ecc43f76 100644 (file)
--- a/label.c
+++ b/label.c
@@ -27,7 +27,13 @@ newtComponent newtLabel(int left, int top, const char * text) {
     struct label * la;
 
     co = malloc(sizeof(*co));
     struct label * la;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
     la = malloc(sizeof(struct label));
     la = malloc(sizeof(struct label));
+    if (la == NULL) {
+       free(co);
+       return NULL;
+    }
     co->data = la;
     co->destroyCallback = NULL;
 
     co->data = la;
     co->destroyCallback = NULL;
 
diff --git a/scale.c b/scale.c
index 35dcdde87802c27d152d44cedf2a5667447bf887..63b42436bfb4eb587b71a3b26557c367bbf5246f 100644 (file)
--- a/scale.c
+++ b/scale.c
@@ -28,7 +28,13 @@ newtComponent newtScale(int left, int top, int width, long long fullValue) {
     struct scale * sc;
 
     co = malloc(sizeof(*co));
     struct scale * sc;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
     sc = malloc(sizeof(struct scale));
     sc = malloc(sizeof(struct scale));
+    if (sc == NULL) {
+       free(co);
+       return NULL;
+    }
     co->data = sc;
     co->destroyCallback = NULL;
 
     co->data = sc;
     co->destroyCallback = NULL;
 
index 0c706b37274e3f4c670b94102eff0d5119647d46..90cd2fe6d88f6c2eafa899a0a26215873c4041e4 100644 (file)
@@ -44,7 +44,13 @@ newtComponent newtVerticalScrollbar(int left, int top, int height,
     struct scrollbar * sb;
 
     co = malloc(sizeof(*co));
     struct scrollbar * sb;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
     sb = malloc(sizeof(*sb));
     sb = malloc(sizeof(*sb));
+    if (sb == NULL) {
+       free(co);
+       return NULL;
+    }
     co->data = sb;
     co->destroyCallback = NULL;
 
     co->data = sb;
     co->destroyCallback = NULL;
 
index b6fedd6bfbf809c423cd9243fc71d305702e5be1..3f7f845685ab43b4aef47d4bdaba7338ebf42e15 100644 (file)
--- a/textbox.c
+++ b/textbox.c
@@ -79,8 +79,15 @@ newtComponent newtTextboxReflowed(int left, int top, char * text, int width,
 
     reflowedText = newtReflowText(text, width, flexDown, flexUp,
                                  &actWidth, &actHeight);
 
     reflowedText = newtReflowText(text, width, flexDown, flexUp,
                                  &actWidth, &actHeight);
+    if (reflowedText == NULL)
+       return NULL;
     
     co = newtTextbox(left, top, actWidth, actHeight, NEWT_FLAG_WRAP);
     
     co = newtTextbox(left, top, actWidth, actHeight, NEWT_FLAG_WRAP);
+    if (co == NULL) {
+       free(reflowedText);
+       return NULL;
+    }
+
     newtTextboxSetText(co, reflowedText);
     free(reflowedText);
 
     newtTextboxSetText(co, reflowedText);
     free(reflowedText);
 
@@ -92,7 +99,13 @@ newtComponent newtTextbox(int left, int top, int width, int height, int flags) {
     struct textbox * tb;
 
     co = malloc(sizeof(*co));
     struct textbox * tb;
 
     co = malloc(sizeof(*co));
+    if (co == NULL)
+       return NULL;
     tb = malloc(sizeof(*tb));
     tb = malloc(sizeof(*tb));
+    if (tb == NULL) {
+       free(co);
+       return NULL;
+    }
     co->data = tb;
 
     if (width < 1)
     co->data = tb;
 
     if (width < 1)
@@ -147,10 +160,15 @@ static char * expandTabs(const char * text) {
     int i;
 
     buf = malloc(bufAlloced + 1);
     int i;
 
     buf = malloc(bufAlloced + 1);
+    if (buf == NULL)
+       return NULL;
+
     for (src = text, dest = buf; *src; src++) {
        if ((bufUsed + 10) > bufAlloced) {
            bufAlloced += strlen(text) / 2;
            buf = realloc(buf, bufAlloced + 1);
     for (src = text, dest = buf; *src; src++) {
        if ((bufUsed + 10) > bufAlloced) {
            bufAlloced += strlen(text) / 2;
            buf = realloc(buf, bufAlloced + 1);
+           if (buf == NULL)
+               return NULL;
            dest = buf + bufUsed;
        }
        if (*src == '\t') {
            dest = buf + bufUsed;
        }
        if (*src == '\t') {
@@ -189,7 +207,10 @@ static void doReflow(const char * text, char ** resultPtr, int width,
            result = malloc(strlen(text) + (strlen(text) / (width - 1)) + 2);
        } else
            result = malloc(strlen(text) * 2 + 2);
            result = malloc(strlen(text) + (strlen(text) / (width - 1)) + 2);
        } else
            result = malloc(strlen(text) * 2 + 2);
+
        *resultPtr = result;
        *resultPtr = result;
+       if (result == NULL)
+           return;
     }
        
     memset(&ps,0,sizeof(mbstate_t));
     }
        
     memset(&ps,0,sizeof(mbstate_t));
@@ -287,6 +308,8 @@ char * newtReflowText(char * text, int width, int flexDown, int flexUp,
        width = 1;
 
     expandedText = expandTabs(text);
        width = 1;
 
     expandedText = expandTabs(text);
+    if (expandedText == NULL)
+       return NULL;
 
     if (flexDown || flexUp) {
        min = width - flexDown;
 
     if (flexDown || flexUp) {
        min = width - flexDown;