AmendHub

Download:

jcs

/

subtext

/

amendments

/

341

logger: Handle malloc failure


jcs made amendment 341 about 1 year ago
--- logger.c Wed Feb 22 16:28:22 2023 +++ logger.c Wed Mar 1 16:47:06 2023 @@ -41,9 +41,12 @@ logger_init(void) Rect bounds = { 0 }; short padding = 5; - logger = xmalloczero(sizeof(struct logger), "logger"); - logger->buffered_logs = xmalloc(LOGGER_BUFFERED_LOG_SIZE, - "logger buffer"); + logger = xmalloczero(sizeof(struct logger)); + if (!logger) + panic("Can't allocate logger"); + logger->buffered_logs = xmalloc(LOGGER_BUFFERED_LOG_SIZE); + if (!logger->buffered_logs) + panic("Can't allocate logger buffer"); logger->buffered_logs[0] = '\0'; logger->autoflush = true; @@ -69,7 +72,9 @@ logger_init(void) bounds.top = bounds.left = 0; logger_layout(&bounds); - focusable = xmalloczero(sizeof(struct focusable), "logger focusable"); + focusable = xmalloczero(sizeof(struct focusable)); + if (!focusable) + panic("Can't create focusable"); focusable->win = logger->win; focusable->cookie = logger; focusable->mouse_down = logger_mouse_down; @@ -77,7 +82,8 @@ logger_init(void) focusable->update = logger_update; focusable->quit = logger_quit; focusable->resume = logger_resume; - add_focusable(focusable); + if (!add_focusable(focusable)) + panic("Can't add focusable"); logger->focusable = focusable; DrawControls(logger->win); @@ -100,10 +106,12 @@ logger_layout(Rect *init_bounds) bounds.right = win_bounds.right - win_bounds.left + 1; bounds.bottom = win_bounds.bottom - win_bounds.top - 14; bounds.left = bounds.right - SCROLLBAR_WIDTH; - if (init) + if (init) { logger->messages_scroller = NewControl(logger->win, &bounds, "\p", true, 1, 1, 1, scrollBarProc, 0L); - else + if (!logger->messages_scroller) + panic("Can't create messages scroller"); + } else (*(logger->messages_scroller))->contrlRect = bounds; /* messages */ @@ -115,6 +123,8 @@ logger_layout(Rect *init_bounds) inset_bounds = bounds; InsetRect(&inset_bounds, 4, 4); logger->messages_te = TEStylNew(&inset_bounds, &bounds); + if (logger->messages_te == NULL) + panic("Can't create logger TE"); (*(logger->messages_te))->caretHook = NullCaretHook; TEActivate(logger->messages_te); } else {