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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Apr 2, 2024 at 16:51 UTC 39bb692152ab8ccd3f955548ad32182c32af529a
1 file changed +4 -20
imap-send.c
+4 -20
@@ -68,9 +68,6 @@ static void imap_warn(const char *, ...);
68
69 static char *next_arg(char **);
70
71 -__attribute__((format (printf, 3, 4)))
72 -static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
73 -
71 static int nfvasprintf(char **strp, const char *fmt, va_list ap)
72 {
73 int len;
@@ -500,19 +497,6 @@ static char *next_arg(char **s)
497 return ret;
498 }
499
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 -
500 static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
501 struct imap_cmd_cb *cb,
502 const char *fmt, va_list ap)
@@ -535,11 +519,11 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
519 get_cmd_result(ctx, NULL);
520
521 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);
523 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) ? "+" : "");
527
528 if (0 < verbosity) {
529 if (imap->num_in_progress)