AmendHub

Download:

jcs

/

subtext

/

amendments

/

338

db: Handle malloc failure, allow blanker_idle_seconds to be 0


jcs made amendment 338 about 1 year ago
--- db.c Wed Mar 1 09:51:32 2023 +++ db.c Thu Mar 2 08:06:08 2023 @@ -80,7 +80,7 @@ struct struct_field config_fields[] = { { "Screen Blanker Idle Seconds", CONFIG_TYPE_SHORT, offsetof(struct config, blanker_idle_seconds), - 1, USHRT_MAX }, + 0, USHRT_MAX }, { "Screen Blanker Runtime Seconds", CONFIG_TYPE_SHORT, offsetof(struct config, blanker_runtime_seconds), 1, USHRT_MAX }, @@ -230,7 +230,9 @@ db_init(Str255 path, short vrefnum, struct bile *bile) WriteResource(lastfileh); DetachResource(lastfileh); - tdb = xmalloczero(sizeof(struct db), "db_init db"); + tdb = xmalloczero(sizeof(struct db)); + if (tdb == NULL) + panic("Can't create db"); tdb->bile = bile; if (db_migrate(tdb, was_new, fullpath) != 0) { @@ -345,7 +347,9 @@ db_migrate(struct db *tdb, short is_new, Str255 fullpa db_config_save(tdb); /* create a default sysop user */ - user = xmalloczero(sizeof(struct user), "db_migrate user"); + user = xmalloczero(sizeof(struct user)); + if (user == NULL) + panic("Can't allocate new user"); strncpy(user->username, "sysop", sizeof(user->username)); user->created_at = Time; user->is_enabled = DB_TRUE; @@ -521,7 +525,7 @@ db_migrate(struct db *tdb, short is_new, Str255 fullpa bile_unmarshall_object(board->bile, board_post_object_fields, nboard_post_object_fields, - data, size, &post, sizeof(post), true, "board_post"); + data, size, &post, sizeof(post), true); xfree(&data); /* zero out junk */ @@ -529,7 +533,7 @@ db_migrate(struct db *tdb, short is_new, Str255 fullpa ret = bile_marshall_object(board->bile, board_post_object_fields, nboard_post_object_fields, - &post, &data, &size, "board_post post"); + &post, &data, &size); if (ret != 0 || size == 0) panic("failed to marshall new post object"); if (bile_write(board->bile, BOARD_POST_RTYPE, @@ -707,7 +711,6 @@ db_cache_boards(struct db *tdb) unsigned long *ids; struct bile_object *obj; char *data = NULL; - char note[MALLOC_NOTE_SIZE]; if (tdb->boards) { for (n = 0; n < tdb->nboards; n++) { @@ -726,8 +729,11 @@ db_cache_boards(struct db *tdb) &ids); if (!tdb->nboards) return; - tdb->boards = xcalloc(tdb->nboards, sizeof(struct board), - "db_cache_boards"); + tdb->boards = xcalloc(tdb->nboards, sizeof(struct board)); + if (tdb->boards == NULL) { + tdb->nboards = 0; + return; + } /* * Read ids first so we have a consistent order, then try to open or @@ -740,10 +746,9 @@ db_cache_boards(struct db *tdb) size = bile_read_alloc(tdb->bile, DB_BOARD_RTYPE, obj->id, &data); - snprintf(note, sizeof(note), "db_cache_boards %ld", obj->id); bile_unmarshall_object(tdb->bile, board_object_fields, nboard_object_fields, data, size, (char *)(&tdb->boards[n]), - sizeof(struct board), true, note); + sizeof(struct board), true); xfree(&data); } @@ -792,7 +797,7 @@ db_board_create(struct db *tdb, struct board *board, b char *data; ret = bile_marshall_object(tdb->bile, board_object_fields, - nboard_object_fields, board, &data, &size, "db_board_create"); + nboard_object_fields, board, &data, &size); if (ret != 0 || size == 0) { warn("db_board_create: failed to marshall object"); return NULL; @@ -843,7 +848,6 @@ db_cache_folders(struct db *tdb) unsigned long *ids; struct bile_object *obj; char *data = NULL; - char note[MALLOC_NOTE_SIZE]; if (tdb->folders) { for (n = 0; n < tdb->nfolders; n++) { @@ -862,9 +866,12 @@ db_cache_folders(struct db *tdb) &ids); if (!tdb->nfolders) return; - tdb->folders = xcalloc(tdb->nfolders, sizeof(struct folder), - "db_cache_folders"); - + tdb->folders = xcalloc(tdb->nfolders, sizeof(struct folder)); + if (tdb->folders == NULL) { + tdb->nfolders = 0; + return; + } + /* * Read ids first so we have a consistent order, then try to open or * fix/recreate each bile, which may change their order in the map @@ -875,10 +882,11 @@ db_cache_folders(struct db *tdb) break; size = bile_read_alloc(tdb->bile, DB_FOLDER_RTYPE, obj->id, &data); - snprintf(note, sizeof(note), "db_cache_folders %ld", obj->id); + if (size == 0 || data == NULL) + break; bile_unmarshall_object(tdb->bile, folder_object_fields, nfolder_object_fields, data, size, (char *)(&tdb->folders[n]), - sizeof(struct folder), true, note); + sizeof(struct folder), true); xfree(&data); } @@ -939,7 +947,7 @@ db_folder_create(struct db *tdb, struct folder *folder char *data; ret = bile_marshall_object(tdb->bile, folder_object_fields, - nfolder_object_fields, folder, &data, &size, "db_folder_create"); + nfolder_object_fields, folder, &data, &size); if (ret != 0 || size == 0) { warn("db_folder_create: failed to marshall object"); return NULL;