#29 improve handling of malloc failures
Closed by mlichvar. Opened by rtgill82.
rtgill82/newt master  into  master

Download 29.patch

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.

Thanks for the patch.

The code doesn't build here:

buttonbar.c: In function newtButtonBarv’:
buttonbar.c:24:17: error: NULL undeclared (first use in this function)
   24 |     if (grid == NULL)

The goto error handling in form.c doesn't work correctly as the form pointer may not be initialized.

Please write free() instead of free (). I know there are inconsistencies in the code, but more common it is without space after the function name.

rebased onto 9b9e7e8936f51f83a7259c5f0a9b4ff909cbd39a

Sorry, the change to buttonbar.c was last minute and I didn't bother test compiling afterwards. Anyways, the requested changes have been made.

The code in form.c still doesn't seem correct. If allocation of co or form fails, free(form->hotKeys); will cause a segfault.

The change in button.c is only fixing formatting, not the actual code, so that should be removed.

rebased onto 9b9e7e8936f51f83a7259c5f0a9b4ff909cbd39a

rebased onto 9b9e7e8936f51f83a7259c5f0a9b4ff909cbd39a

Sorry, fixed.

Applied with a small fix to not read past the grid field array and also free reflowedText in one of the error paths.

Thanks!

Pull-Request has been closed by mlichvar

I'm afraid there may be a logic mistake in freeing the columns in the grid field array. The columns are allocated in reverse order, so the logic I used before should stop at the NULL returned by the failed malloc(). Using a for loop counts forward attempting to to free() the unallocated fields.

It stopped at NULL, but it started reading at grid->fields[grid->cols], outside of the array, possibly terminating early or freeing an invalid pointer. The code I replaced this with is simpler, the loop just goes back over the pointers that were already allocated. They cannot be NULL because the error path was not reached earlier, i.e. only the last one is NULL.

Let me know if you see any issues.

Ok, I see what's happening. And I realize what my mistake was. For some reason I thought you were re-initializing cols as 0 for the for loop, but you were continuing forward from where it left off. And I realize that I had an off by one error, starting just outside the end of the array.

Metadata