AmendHub

Download:

jcs

/

subtext

/

amendments

/

342

mail: Handle malloc failure, use grow_to_fit


jcs made amendment 342 about 1 year ago
--- mail.c Thu Feb 23 17:10:10 2023 +++ mail.c Thu Mar 2 09:28:03 2023 @@ -249,12 +249,21 @@ mail_compose(struct session *s, char *initial_to, char return; } - if (initial_to) - to_username = xstrdup(initial_to, "mail_compose"); - if (initial_subject) - msg.subject = xstrdup(initial_subject, "mail_compose"); - if (initial_body) - msg.body = xstrdup(initial_body, "mail_compose"); + if (initial_to) { + to_username = xstrdup(initial_to); + if (to_username == NULL) + goto mail_compose_done; + } + if (initial_subject) { + msg.subject = xstrdup(initial_subject); + if (msg.subject == NULL) + goto mail_compose_done; + } + if (initial_body) { + msg.body = xstrdup(initial_body); + if (msg.body == NULL) + goto mail_compose_done; + } session_printf(s, "{{B}}Compose New Private Mail{{/B}}\r\n"); session_printf(s, "{{B}}From: {{/B}} %s\r\n", s->user->username); @@ -421,8 +430,7 @@ mail_list(struct session *s, size_t nmail_ids, unsigne if (size == 0) break; bile_unmarshall_object(db->mail_bile, mail_object_fields, - nitems(mail_object_fields), data, size, &msg, sizeof(msg), true, - "mail_list"); + nitems(mail_object_fields), data, size, &msg, sizeof(msg), true); xfree(&data); user = user_username(msg.sender_user_id); @@ -474,8 +482,7 @@ mail_read(struct session *s, unsigned long id, short i } bile_unmarshall_object(db->mail_bile, mail_object_fields, - nitems(mail_object_fields), data, size, &msg, sizeof(msg), true, - "mail_read"); + nitems(mail_object_fields), data, size, &msg, sizeof(msg), true); xfree(&data); sender = user_username(msg.sender_user_id); @@ -516,8 +523,9 @@ mail_read(struct session *s, unsigned long id, short i if (!sender) break; - reply_subject = xmalloc(strlen(msg.subject) + 5, - "mail_read subject"); + reply_subject = xmalloc(strlen(msg.subject) + 5); + if (reply_subject == NULL) + break; if (strncmp(msg.subject, "Re:", 3) == 0) strlcpy(reply_subject, msg.subject, strlen(msg.subject) + 1); @@ -581,7 +589,7 @@ mail_save(struct session *s, struct mail_message *msg) msg->id = bile_next_id(db->mail_bile, MAIL_SPOOL_MESSAGE_RTYPE); ret = bile_marshall_object(db->mail_bile, mail_object_fields, - nitems(mail_object_fields), msg, &data, &size, "mail_save"); + nitems(mail_object_fields), msg, &data, &size); if (ret != 0 || size == 0) { warn("mail_save: failed to marshall object"); return -1; @@ -610,7 +618,7 @@ mail_find_ids_for_user(struct user *user, size_t *nmai mail_ids_size = sizeof(long) * 16; if (mail_ids != NULL) - *mail_ids = xmalloc(mail_ids_size, "mail_find_ids_for_user ids"); + *mail_ids = xmalloc(mail_ids_size); if (nmail_ids != NULL) *nmail_ids = 0; @@ -628,9 +636,10 @@ mail_find_ids_for_user(struct user *user, size_t *nmai if (only_unread) { size = bile_read_alloc(db->mail_bile, MAIL_SPOOL_MESSAGE_RTYPE, id, &data); + if (size == 0) + break; bile_unmarshall_object(db->mail_bile, mail_object_fields, - nmail_object_fields, data, size, &msg, sizeof(msg), false, - "mail_find_ids_for_user"); + nmail_object_fields, data, size, &msg, sizeof(msg), false); xfree(&data); read = msg.read; if (read) @@ -638,9 +647,10 @@ mail_find_ids_for_user(struct user *user, size_t *nmai } if (mail_ids != NULL) { - EXPAND_TO_FIT(*mail_ids, mail_ids_size, + if (!grow_to_fit(mail_ids, &mail_ids_size, (nmsgs_for_user + 1) * sizeof(long), sizeof(long), - sizeof(long) * 16); + sizeof(long) * 16)) + break; (*mail_ids)[nmsgs_for_user] = id; }