imap-send: handle NULL return of next_arg()

next_arg() returns NULL if it runs out of arguments. Most call sites already handle that gracefully. Check in the remaining cases as well. Replace the NULL pointer with an empty string at the bottom of get_cmd_result() -- it's nicely reported as an unexpected response a few lines down. Error out explicitly at the remaining sites. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Nov 1, 2017 at 18:03 UTC f54c5bd40c0e215fc0ba196d74b092ef3ec02273
1 file changed +12 -1
imap-send.c
+12 -1
@@ -692,6 +692,10 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
692 }
693 *p++ = 0;
694 arg = next_arg(&s);
695 + if (!arg) {
696 + fprintf(stderr, "IMAP error: empty response code\n");
697 + return RESP_BAD;
698 + }
699 if (!strcmp("UIDVALIDITY", arg)) {
700 if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
701 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
@@ -724,7 +728,8 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
728 {
729 struct imap *imap = ctx->imap;
730 struct imap_cmd *cmdp, **pcmdp;
727 - char *cmd, *arg, *arg1;
731 + char *cmd;
732 + const char *arg, *arg1;
733 int n, resp, resp2, tag;
734
735 for (;;) {
@@ -732,6 +737,10 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
737 return RESP_BAD;
738
739 arg = next_arg(&cmd);
740 + if (!arg) {
741 + fprintf(stderr, "IMAP error: empty response\n");
742 + return RESP_BAD;
743 + }
744 if (*arg == '*') {
745 arg = next_arg(&cmd);
746 if (!arg) {
@@ -807,6 +816,8 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
816 if (cmdp->cb.cont || cmdp->cb.data)
817 imap->literal_pending = 0;
818 arg = next_arg(&cmd);
819 + if (!arg)
820 + arg = "";
821 if (!strcmp("OK", arg))
822 resp = DRV_OK;
823 else {