Skip to content

Commit 39bb692

Browse files
rscharfegitster
authored andcommitted
imap-send: use xsnprintf to format command
nfsnprintf() wraps vsnprintf(3) and reports attempts to use too small a buffer using BUG(), just like xsnprintf(). It has an extra check that makes sure the buffer size (converted to int) is positive. vsnprintf(3) is supposed to handle a buffer size of zero or bigger than INT_MAX just fine, so this extra comparison doesn't make us any safer. If a platform has a broken implementation, we'd need to work around it in our compat code. Call xsnprintf() instead to reduce code duplication and make the caller slightly more readable by using this more common helper. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c2a3fd commit 39bb692

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

imap-send.c

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ static void imap_warn(const char *, ...);
6868

6969
static char *next_arg(char **);
7070

71-
__attribute__((format (printf, 3, 4)))
72-
static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
73-
7471
static int nfvasprintf(char **strp, const char *fmt, va_list ap)
7572
{
7673
int len;
@@ -500,19 +497,6 @@ static char *next_arg(char **s)
500497
return ret;
501498
}
502499

503-
__attribute__((format (printf, 3, 4)))
504-
static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
505-
{
506-
int ret;
507-
va_list va;
508-
509-
va_start(va, fmt);
510-
if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
511-
BUG("buffer too small. Please report a bug.");
512-
va_end(va);
513-
return ret;
514-
}
515-
516500
static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
517501
struct imap_cmd_cb *cb,
518502
const char *fmt, va_list ap)
@@ -535,11 +519,11 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
535519
get_cmd_result(ctx, NULL);
536520

537521
if (!cmd->cb.data)
538-
bufl = nfsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
522+
bufl = xsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
539523
else
540-
bufl = nfsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
541-
cmd->tag, cmd->cmd, cmd->cb.dlen,
542-
CAP(LITERALPLUS) ? "+" : "");
524+
bufl = xsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
525+
cmd->tag, cmd->cmd, cmd->cb.dlen,
526+
CAP(LITERALPLUS) ? "+" : "");
543527

544528
if (0 < verbosity) {
545529
if (imap->num_in_progress)

0 commit comments

Comments
 (0)