imap: replace atoi() with strtol_i() for UIDVALIDITY and UIDNEXT parsing

Replace unsafe uses of atoi() with strtol_i() to improve error handling when parsing UIDVALIDITY, UIDNEXT, and APPENDUID in IMAP commands. Invalid values, such as those with letters, now trigger error messages and prevent malformed status responses. I did not add any test for this commit as we do not have any test for git-imap-send(1) at this point. Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Usman Akinyemi committed Oct 24, 2024 at 00:24 UTC e226ba81a2520263580721b1527794428923cd67
1 file changed +8 -5
imap-send.c
+8 -5
@@ -668,12 +668,12 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
668 return RESP_BAD;
669 }
670 if (!strcmp("UIDVALIDITY", arg)) {
671 - if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
671 + if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) || !ctx->uidvalidity) {
672 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
673 return RESP_BAD;
674 }
675 } else if (!strcmp("UIDNEXT", arg)) {
676 - if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
676 + if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &imap->uidnext) || !imap->uidnext) {
677 fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
678 return RESP_BAD;
679 }
@@ -686,8 +686,8 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
686 for (; isspace((unsigned char)*p); p++);
687 fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
688 } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
689 - if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) ||
690 - !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
689 + if (!(arg = next_arg(&s)) || strtol_i(arg, 10, &ctx->uidvalidity) || !ctx->uidvalidity ||
690 + !(arg = next_arg(&s)) || strtol_i(arg, 10, (int *)cb->ctx) || !cb->ctx) {
691 fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
692 return RESP_BAD;
693 }
@@ -773,7 +773,10 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
773 if (!tcmd)
774 return DRV_OK;
775 } else {
776 - tag = atoi(arg);
776 + if (strtol_i(arg, 10, &tag)) {
777 + fprintf(stderr, "IMAP error: malformed tag %s\n", arg);
778 + return RESP_BAD;
779 + }
780 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
781 if (cmdp->tag == tag)
782 goto gottag;