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()]
}
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);
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;
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;
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;
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;
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;
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;
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;
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) {
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);
}
}
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,
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,
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,
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;
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;
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;
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);
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)
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') {
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);
+ if (result == NULL)
+ return;
}
memset(&ps,0,sizeof(mbstate_t));
}
memset(&ps,0,sizeof(mbstate_t));
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;