Skip to content

Commit f54c5bd

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9752ad0 commit f54c5bd

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

imap-send.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,10 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
692692
}
693693
*p++ = 0;
694694
arg = next_arg(&s);
695+
if (!arg) {
696+
fprintf(stderr, "IMAP error: empty response code\n");
697+
return RESP_BAD;
698+
}
695699
if (!strcmp("UIDVALIDITY", arg)) {
696700
if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
697701
fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
@@ -724,14 +728,19 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
724728
{
725729
struct imap *imap = ctx->imap;
726730
struct imap_cmd *cmdp, **pcmdp;
727-
char *cmd, *arg, *arg1;
731+
char *cmd;
732+
const char *arg, *arg1;
728733
int n, resp, resp2, tag;
729734

730735
for (;;) {
731736
if (buffer_gets(&imap->buf, &cmd))
732737
return RESP_BAD;
733738

734739
arg = next_arg(&cmd);
740+
if (!arg) {
741+
fprintf(stderr, "IMAP error: empty response\n");
742+
return RESP_BAD;
743+
}
735744
if (*arg == '*') {
736745
arg = next_arg(&cmd);
737746
if (!arg) {
@@ -807,6 +816,8 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
807816
if (cmdp->cb.cont || cmdp->cb.data)
808817
imap->literal_pending = 0;
809818
arg = next_arg(&cmd);
819+
if (!arg)
820+
arg = "";
810821
if (!strcmp("OK", arg))
811822
resp = DRV_OK;
812823
else {

0 commit comments

Comments
 (0)