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

[PATCH maint] imap-send: Fix sprintf usage

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Aug 7, 2010, 23:09 UTC
Message-ID
<20100807230945.GA3427@burratino>
In-Reply-To
<AANLkTim4CHdVLinkw1EjXB74OJ+YW-ri4GzHMNhRd+Cy@mail.gmail.com>
From: Ævar Arnfjörð Bjarmason <avarab@gmail.com>

When composing a command for the imap server, imap-send uses a single nfsnprintf() invocation for brevity instead of dealing separately with the case when there is a message to be sent and the case when there isn’t. The unused argument in the second case, while valid, is confusing for static analyzers and human readers.

v1.6.4-rc0~117 (imap-send: add support for IPv6, 2009-05-25) mistakenly used %hu as the format for an int “port”, by analogy with existing usage for the unsigned short “addr.sin_port”.  Use %d instead.

Noticed with clang.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Ævar Arnfjörð Bjarmason wrote:
Show 5 quoted lines
> I wasn't sure whether it needed to be %hu for the purposes of the
> snprintf() call. I.e. that the resulting contents of portstr might be
> different on some systems.
> 
> Maybe they won't be, then we could just use %d.
It’s just a nonnegative integer. :)
Here’s the updated patch.  Untested.
 imap-send.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/imap-send.c b/imap-send.c
index 1a577a0..3a02e89 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -543,9 +543,13 @@ static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
 	while (imap->literal_pending)
 		get_cmd_result(ctx, NULL);
 
-	bufl = nfsnprintf(buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
-			   "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
-			   cmd->tag, cmd->cmd, cmd->cb.dlen);
+	if (!cmd->cb.data)
+		bufl = nfsnprintf(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) ? "+" : "");
+
 	if (Verbose) {
 		if (imap->num_in_progress)
 			printf("(%d in progress) ", imap->num_in_progress);
@@ -1086,7 +1090,7 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
 		int gai;
 		char portstr[6];
 
-		snprintf(portstr, sizeof(portstr), "%hu", srvc->port);
+		snprintf(portstr, sizeof(portstr), "%d", srvc->port);
 
 		memset(&hints, 0, sizeof(hints));
 		hints.ai_socktype = SOCK_STREAM;
-- 
1.7.2.1.544.ga752d.dirty
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 4 of 5 in “imap-send: Code correctness flagged by clang”
  1. imap-send: Code correctness flagged by clangÆvar Arnfjörð Bjarmason, Aug 7, 2010
  2. Jonathan NiederAug 7, 2010
  3. Ævar Arnfjörð BjarmasonAug 7, 2010
  4. imap-send: Fix sprintf usageJonathan Nieder, Aug 7, 2010
  5. Ævar Arnfjörð BjarmasonAug 7, 2010

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.