git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] imap-send: use xsnprintf to format command

From
René Scharfe <l.s.r@web.de>
Date
Apr 2, 2024, 14:51 UTC
Message-ID
<f9ad9f41-5b9b-474e-9818-f91fc937daae@web.de>

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>
---
Uploaded this message using the patched command, but that's all the
testing I did.
 imap-send.c | 24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
diff --git a/imap-send.c b/imap-send.c
index f2e1947e63..4caa8668e6 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -68,9 +68,6 @@ static void imap_warn(const char *, ...);

 static char *next_arg(char **);

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

-__attribute__((format (printf, 3, 4)))
-static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
-{
-	int ret;
-	va_list va;
-
-	va_start(va, fmt);
-	if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
-		BUG("buffer too small. Please report a bug.");
-	va_end(va);
-	return ret;
-}
-
 static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
 				       struct imap_cmd_cb *cb,
 				       const char *fmt, va_list ap)
@@ -535,11 +519,11 @@ static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
 		get_cmd_result(ctx, NULL);

 	if (!cmd->cb.data)
-		bufl = nfsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
+		bufl = xsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
 	else
-		bufl = nfsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
-				  cmd->tag, cmd->cmd, cmd->cb.dlen,
-				  CAP(LITERALPLUS) ? "+" : "");
+		bufl = xsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
+				 cmd->tag, cmd->cmd, cmd->cb.dlen,
+				 CAP(LITERALPLUS) ? "+" : "");

 	if (0 < verbosity) {
 		if (imap->num_in_progress)
--
2.44.0
Next: Jeff King
Message 1 of 4 in “imap-send: use xsnprintf to format command”
  1. imap-send: use xsnprintf to format commandRené Scharfe, Apr 2, 2024
  2. Jeff KingApr 3, 2024
  3. René ScharfeApr 3, 2024
  4. Jeff KingApr 3, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.