AmendHub

jcs

/

subtext

/

amendments

/

232

board: Fix some memory leaks, pass malloc notes to bile


jcs made amendment 232 2 months ago
--- board.c Sun Jul 31 18:15:08 2022 +++ board.c Mon Aug 1 15:25:57 2022 @@ -296,27 +296,30 @@ board_list_posts(struct session *s, struct board *boar &data); bile_unmarshall_object(board->bile, board_post_object_fields, nboard_post_object_fields, data, size, &post, sizeof(post), - false); + false, "board_list_posts"); xfree(&data); if (post.thread_id != thread.thread_id) { if (thread.thread_id) { - xfree(&thread.subject); - xfree(&thread.post_ids); - xfree(&thread.parent_post_ids); + if (thread.subject != NULL) + xfree(&thread.subject); + if (thread.post_ids != NULL) + xfree(&thread.post_ids); + if (thread.parent_post_ids != NULL) + xfree(&thread.parent_post_ids); } size = bile_read_alloc(board->bile, BOARD_THREAD_RTYPE, post.thread_id, &data); bile_unmarshall_object(board->bile, board_thread_object_fields, nboard_thread_object_fields, data, size, &thread, - sizeof(thread), true); + sizeof(thread), true, "board_list_posts"); xfree(&data); for (j = 0; j < nitems(indent_parent_ids); j++) indent_parent_ids[j] = 0; } - user = user_find_username(post.sender_user_id); + user = user_username(post.sender_user_id); strftime(time, sizeof(time), "%b %d", localtime(&post.time)); if (post.parent_post_id == 0) { @@ -557,7 +560,8 @@ board_post_read(struct session *s, struct board *board panic("failed fetching message %ld: %d", id, bile_error(board->bile)); bile_unmarshall_object(board->bile, board_post_object_fields, - nboard_post_object_fields, data, size, &post, sizeof(post), true); + nboard_post_object_fields, data, size, &post, sizeof(post), true, + "board_post_read post"); xfree(&data); size = bile_read_alloc(board->bile, BOARD_THREAD_RTYPE, @@ -567,7 +571,7 @@ board_post_read(struct session *s, struct board *board bile_error(board->bile)); bile_unmarshall_object(board->bile, board_thread_object_fields, nboard_thread_object_fields, data, size, &thread, sizeof(thread), - true); + true, "board_post_read thread"); xfree(&data); dopts = xmalloc(sizeof(opts), "board_post_read opts"); @@ -578,7 +582,7 @@ board_post_read(struct session *s, struct board *board dopts[1].key[0] = '\0'; } - sender = user_find_username(post.sender_user_id); + sender = user_username(post.sender_user_id); strftime(time, sizeof(time), "%Y-%m-%d %H:%M:%S", localtime(&post.time)); @@ -662,6 +666,17 @@ board_post_read(struct session *s, struct board *board } } + xfree(&dopts); + + if (post.body != NULL) + xfree(&post.body); + if (thread.subject != NULL) + xfree(&thread.subject); + if (thread.post_ids != NULL) + xfree(&thread.post_ids); + if (thread.parent_post_ids != NULL) + xfree(&thread.parent_post_ids); + return ret; } @@ -702,7 +717,7 @@ board_find_post_ids(struct board *board, size_t *npost bile_read_alloc(board->bile, BOARD_THREAD_RTYPE, o->id, &data); bile_unmarshall_object(board->bile, board_thread_object_fields, nboard_thread_object_fields, data, o->size, &thread, - sizeof(thread), false); + sizeof(thread), false, "board_find_post_ids 1"); xfree(&data); xfree(&o); @@ -733,7 +748,7 @@ board_find_post_ids(struct board *board, size_t *npost thread_map[j].id, &data); bile_unmarshall_object(board->bile, board_thread_object_fields, nboard_thread_object_fields, data, size, &thread, sizeof(thread), - true); + true, "board_find_post_ids 2"); xfree(&data); for (i = 0; i < thread.nposts; i++) { @@ -750,8 +765,12 @@ board_find_post_ids(struct board *board, size_t *npost break; } - xfree(&thread.post_ids); - xfree(&thread.subject); + if (thread.subject != NULL) + xfree(&thread.subject); + if (thread.post_ids != NULL) + xfree(&thread.post_ids); + if (thread.parent_post_ids != NULL) + xfree(&thread.parent_post_ids); if (*npost_ids >= limit) break; @@ -783,7 +802,8 @@ board_post_create(struct board *board, struct board_th post->time = Time; ret = bile_marshall_object(board->bile, board_post_object_fields, - nboard_post_object_fields, post, &data, &size); + nboard_post_object_fields, post, &data, &size, + "board_post_create post"); if (ret != 0 || size == 0) { warn("failed to marshall new post object"); post->id = 0; @@ -796,7 +816,8 @@ board_post_create(struct board *board, struct board_th xfree(&data); goto done; } - + xfree(&data); + thread->last_post_at = post->time; thread->nposts++; thread->post_ids = xreallocarray(thread->post_ids, thread->nposts, @@ -827,7 +848,8 @@ board_post_create(struct board *board, struct board_th thread->parent_post_ids[insert] = post->parent_post_id; ret = bile_marshall_object(board->bile, board_thread_object_fields, - nboard_thread_object_fields, thread, &data, &size); + nboard_thread_object_fields, thread, &data, &size, + "board_post_create thread"); if (ret != 0 || size == 0) { warn("failed to marshall thread object"); post->id = 0; @@ -837,6 +859,7 @@ board_post_create(struct board *board, struct board_th data, size) != size) { warn("bile_write of thread failed! %d", bile_error(board->bile)); post->id = 0; + xfree(&data); goto done; } xfree(&data); @@ -903,7 +926,8 @@ board_delete_post(struct session *s, struct board *boa thread->parent_post_ids = new_parent_post_ids; ret = bile_marshall_object(board->bile, board_thread_object_fields, - nboard_thread_object_fields, thread, &data, &size); + nboard_thread_object_fields, thread, &data, &size, + "board_delete_post"); if (ret != 0 || size == 0) { warn("failed to marshall thread object during post delete"); return; @@ -932,7 +956,7 @@ board_delete_post(struct session *s, struct board *boa post->body_size = strlen(post->body) + 1; ret = bile_marshall_object(board->bile, board_post_object_fields, - nboard_post_object_fields, post, &data, &size); + nboard_post_object_fields, post, &data, &size, "board_delete_post"); if (ret != 0 || size == 0) { warn("failed to marshall updated post object"); return; @@ -944,4 +968,6 @@ board_delete_post(struct session *s, struct board *boa xfree(&data); return; } + + xfree(&data); }