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

[PATCH] git-name-rev: don't use printf without format

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Aug 3, 2008, 13:44 UTC
Message-ID
<4895B641.1050500@lsrfire.ath.cx>
In-Reply-To
<7vfxpnmgkc.fsf@gitster.siamese.dyndns.org>

printf() without an explicit format string is not a good coding practise, unless the printed string is guaranteed to not contain percent signs. While fixing this, we might as well combine the calls to fwrite() and printf().

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 builtin-name-rev.c |   12 +++++-------
 1 files changed, 5 insertions(+), 7 deletions(-)
diff --git a/builtin-name-rev.c b/builtin-name-rev.c
index 7055ac3..08c8aab 100644
--- a/builtin-name-rev.c
+++ b/builtin-name-rev.c
@@ -189,6 +189,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
 			unsigned char sha1[40];
 			const char *name = NULL;
 			char c = *(p+1);
+			int p_len = p - p_start + 1;
 
 			forty = 0;
 
@@ -204,13 +205,10 @@ static void name_rev_line(char *p, struct name_ref_data *data)
 			if (!name)
 				continue;
 
-			if (data->name_only) {
-				fwrite(p_start, p - p_start + 1 - 40, 1, stdout);
-				printf(name);
-			} else {
-				fwrite(p_start, p - p_start + 1, 1, stdout);
-				printf(" (%s)", name);
-			}
+			if (data->name_only)
+				printf("%.*s%s", p_len - 40, p_start, name);
+			else
+				printf("%.*s (%s)", p_len, p_start, name);
 			p_start = p + 1;
 		}
 	}
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 8 in “git-name-rev: allow --name-only in combination with --stdin”
  1. git-name-rev: allow --name-only in combination with --stdinPieter de Bie, Jul 31, 2008
  2. Junio C HamanoAug 1, 2008
  3. Johannes SchindelinAug 1, 2008
  4. builtin-name-rev: refactor stdin handling to its own functionPieter de Bie, Aug 1, 2008
  5. Junio C HamanoAug 1, 2008
  6. Junio C HamanoAug 2, 2008
  7. git-name-rev: don't use printf without formatRené Scharfe, Aug 3, 2008
  8. Junio C HamanoAug 3, 2008

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.