jcs
/subtext
/amendments
/345
settings: Handle malloc failure, fix printf format
jcs made amendment 345 about 1 year ago
--- settings.c Tue Feb 28 09:35:56 2023
+++ settings.c Thu Mar 2 08:07:38 2023
@@ -58,7 +58,9 @@ struct_editor(struct session *s, const struct struct_f
session_printf(s, "{{B}}Editor: %s{{/B}}\r\n", title);
session_flush(s);
- new_data = xmalloc(dsize, "struct_editor");
+ new_data = xmalloc(dsize);
+ if (new_data == NULL)
+ goto done;
memcpy(new_data, data, dsize);
print_options:
@@ -209,14 +211,14 @@ get_input:
lval = atol(input);
if (lval < sf->min) {
session_printf(s,
- "%s must be at least %d (^C to cancel)\r\n", sf->name,
+ "%s must be at least %lu (^C to cancel)\r\n", sf->name,
sf->min);
xfree(&input);
goto get_input;
}
if (lval > sf->max) {
session_printf(s,
- "%s must be less than %d (^C to cancel)\r\n", sf->name,
+ "%s must be less than %lu (^C to cancel)\r\n", sf->name,
sf->max);
xfree(&input);
goto get_input;
@@ -339,16 +341,20 @@ view_editor_show(size_t id, char *title)
((screenBits.bounds.bottom - height) / 2);
bounds.bottom = bounds.top + height;
- view_editor = xmalloczero(sizeof(struct view_editor),
- "view_editor_show editor");
+ view_editor = xmalloczero(sizeof(struct view_editor));
+ if (view_editor == NULL)
+ return;
view_editor->view_id = id;
CtoPstr(title);
view_editor->win = NewWindow(0L, &bounds, title, false, noGrowDocProc,
(WindowPtr)-1L, true, 0);
PtoCstr(title);
- if (!view_editor->win)
- panic("Can't create window");
+ if (!view_editor->win) {
+ warn("Can't create window");
+ xfree(&view_editor);
+ return;
+ }
SetPort(view_editor->win);
/* add save button */
@@ -360,7 +366,11 @@ view_editor_show(size_t id, char *title)
bounds.top = bounds.bottom - 20;
view_editor->save_button = NewControl(view_editor->win, &bounds,
"\pSave", true, 1, 1, 1, pushButProc, 0L);
-
+ if (view_editor->save_button == NULL) {
+ warn("Failed to create save button");
+ goto fail;
+ }
+
/* add text box */
bounds.bottom = bounds.top - padding - SCROLLBAR_WIDTH;
bounds.top = padding;
@@ -372,6 +382,10 @@ view_editor_show(size_t id, char *title)
TextFont(monaco);
TextSize(9);
view_editor->te = TEStylNew(&te_bounds, &bounds);
+ if (view_editor->te == NULL) {
+ warn("Failed to create TE");
+ goto fail;
+ }
style.tsFont = monaco;
style.tsSize = 9;
TESetStyle(doFont | doSize, &style, false, view_editor->te);
@@ -381,6 +395,8 @@ view_editor_show(size_t id, char *title)
TEActivate(view_editor->te);
vsize = bile_read_alloc(db->bile, DB_TEXT_TYPE, id, &view);
+ if (bile_error(db->bile) == BILE_ERR_NO_MEMORY)
+ goto fail;
if (view) {
TESetText(view, vsize, view_editor->te);
HLock(view_editor->te);
@@ -395,6 +411,10 @@ view_editor_show(size_t id, char *title)
bounds.top--;
view_editor->vert_scroller = NewControl(view_editor->win, &bounds,
"\p", true, 1, 1, 1, scrollBarProc, 0L);
+ if (view_editor->vert_scroller == NULL) {
+ warn("Failed to create scroller");
+ goto fail;
+ }
bounds.left = (*(view_editor->te))->viewRect.left - 1;
bounds.right = (*(view_editor->te))->viewRect.right + 1;
@@ -402,20 +422,37 @@ view_editor_show(size_t id, char *title)
bounds.bottom = bounds.top + SCROLLBAR_WIDTH;
view_editor->horiz_scroller = NewControl(view_editor->win, &bounds,
"\p", true, 1, 1, 1, scrollBarProc, 0L);
+ if (view_editor->horiz_scroller == NULL) {
+ warn("Failed to create scroller");
+ goto fail;
+ }
- focusable = xmalloczero(sizeof(struct focusable), "focusable");
+ focusable = xmalloczero(sizeof(struct focusable));
+ if (focusable == NULL)
+ goto fail;
focusable->win = view_editor->win;
focusable->cookie = view_editor;
focusable->key_down = view_editor_key_down;
focusable->mouse_down = view_editor_mouse_down;
focusable->update = view_editor_update;
focusable->close = view_editor_close;
- add_focusable(focusable);
+ if (!add_focusable(focusable))
+ goto fail;
UpdateScrollbarForTE(view_editor->win, view_editor->vert_scroller,
view_editor->te, false);
UpdateScrollbarForTE(view_editor->win, view_editor->horiz_scroller,
view_editor->te, false);
+
+ return;
+
+fail:
+ if (focusable)
+ xfree(&focusable);
+ if (view_editor->te)
+ TEDispose(view_editor->te);
+ DisposeWindow(view_editor);
+ xfree(&view_editor);
}
void