Merge branch 'rs/imap-send-simplify-cmd-issuing-codepath'

Code simplification.

* rs/imap-send-simplify-cmd-issuing-codepath:
  imap-send: increase command size limit
This commit is contained in:
Junio C Hamano
2024-04-23 11:52:41 -07:00

View File

@ -68,20 +68,6 @@ static void imap_warn(const char *, ...);
static char *next_arg(char **); static char *next_arg(char **);
static int nfvasprintf(char **strp, const char *fmt, va_list ap)
{
int len;
char tmp[8192];
len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
if (len < 0)
die("Fatal: Out of memory");
if (len >= sizeof(tmp))
die("imap command overflow!");
*strp = xmemdupz(tmp, len);
return len;
}
struct imap_server_conf { struct imap_server_conf {
const char *name; const char *name;
const char *tunnel; const char *tunnel;
@ -503,11 +489,11 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
{ {
struct imap *imap = ctx->imap; struct imap *imap = ctx->imap;
struct imap_cmd *cmd; struct imap_cmd *cmd;
int n, bufl; int n;
char buf[1024]; struct strbuf buf = STRBUF_INIT;
cmd = xmalloc(sizeof(struct imap_cmd)); cmd = xmalloc(sizeof(struct imap_cmd));
nfvasprintf(&cmd->cmd, fmt, ap); cmd->cmd = xstrvfmt(fmt, ap);
cmd->tag = ++imap->nexttag; cmd->tag = ++imap->nexttag;
if (cb) if (cb)
@ -519,27 +505,30 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
get_cmd_result(ctx, NULL); get_cmd_result(ctx, NULL);
if (!cmd->cb.data) if (!cmd->cb.data)
bufl = xsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd); strbuf_addf(&buf, "%d %s\r\n", cmd->tag, cmd->cmd);
else else
bufl = xsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n", strbuf_addf(&buf, "%d %s{%d%s}\r\n", cmd->tag, cmd->cmd,
cmd->tag, cmd->cmd, cmd->cb.dlen, cmd->cb.dlen, CAP(LITERALPLUS) ? "+" : "");
CAP(LITERALPLUS) ? "+" : ""); if (buf.len > INT_MAX)
die("imap command overflow!");
if (0 < verbosity) { if (0 < verbosity) {
if (imap->num_in_progress) if (imap->num_in_progress)
printf("(%d in progress) ", imap->num_in_progress); printf("(%d in progress) ", imap->num_in_progress);
if (!starts_with(cmd->cmd, "LOGIN")) if (!starts_with(cmd->cmd, "LOGIN"))
printf(">>> %s", buf); printf(">>> %s", buf.buf);
else else
printf(">>> %d LOGIN <user> <pass>\n", cmd->tag); printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
} }
if (socket_write(&imap->buf.sock, buf, bufl) != bufl) { if (socket_write(&imap->buf.sock, buf.buf, buf.len) != buf.len) {
free(cmd->cmd); free(cmd->cmd);
free(cmd); free(cmd);
if (cb) if (cb)
free(cb->data); free(cb->data);
strbuf_release(&buf);
return NULL; return NULL;
} }
strbuf_release(&buf);
if (cmd->cb.data) { if (cmd->cb.data) {
if (CAP(LITERALPLUS)) { if (CAP(LITERALPLUS)) {
n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen); n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);