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

Re: [PATCH 2/2] avoid using sha1_to_hex output as printf format

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 8, 2016, 17:02 UTC
Message-ID
<xmqqinwgm5aa.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160708103515.GA19705@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 22 quoted lines
> On Fri, Jul 08, 2016 at 05:25:26AM -0400, Jeff King wrote:
>
>> diff --git a/commit.c b/commit.c
>> index 3f4f371..9603379 100644
>> --- a/commit.c
>> +++ b/commit.c
>> @@ -1623,7 +1623,7 @@ void print_commit_list(struct commit_list *list,
>>  {
>>  	for ( ; list; list = list->next) {
>>  		const char *format = list->next ? format_cur : format_last;
>> -		printf(format, oid_to_hex(&list->item->object.oid));
>> +		printf(format, "%s", oid_to_hex(&list->item->object.oid));
>
> Urgh, this second hunk is clearly bogus. This is a -Wformat-nonliteral
> problem, but not because of oid_to_hex(), but rather because of
> "format". :-/
>
> Here's a corrected patch. But as this has demonstrated the dangers of
> churn, and as it doesn't really get us meaningfully closer to being able
> to use -Wformat-nonliteral, perhaps the best course of action is to just
> drop it (I do think the "walker_say" patch has more inherent value as a
> cleanup, though).

Hmm. While both do look correct, and it is a no-brainer to take this (corrected) patch, I am not sure how much we care about walkers these days.

As to the hunk to commit.c that was dropped in this round, the only caller of print_commit_list() is bisect.c, and it passes "%s\n" to format_cur and format_last, it seems, so that suggests a more obvious direction for cleaning things up, I would say.

Show 25 quoted lines
> -- >8 --
> Subject: [PATCH] avoid using sha1_to_hex output as printf format
>
> We know that it should not contain any percent-signs, but
> it's a good habit not to feed non-literals to printf
> formatters.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>  builtin/worktree.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index e866844..cce555c 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -262,7 +262,7 @@ static int add_worktree(const char *path, const char *refname,
>  	 */
>  	strbuf_reset(&sb);
>  	strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
> -	write_file(sb.buf, sha1_to_hex(null_sha1));
> +	write_file(sb.buf, "%s", sha1_to_hex(null_sha1));
>  	strbuf_reset(&sb);
>  	strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
>  	write_file(sb.buf, "../..");
Previous: Jeff KingNext: Junio C Hamano
Message 17 of 27 in “am: ignore return value of write_file()”
  1. am: ignore return value of write_file()René Scharfe, Jul 7, 2016
  2. Jeff KingJul 7, 2016
  3. Johannes SchindelinJul 8, 2016
  4. Jeff KingJul 8, 2016
  5. 0/8 write_file cleanupsJeff King, Jul 8, 2016
  6. 1/8 config: fix bogus fd check when setting up default configJeff King, Jul 8, 2016
  7. 2/8 am: ignore return value of write_file()Jeff King, Jul 8, 2016
  8. 3/8 branch: use non-gentle write_file for branch descriptionJeff King, Jul 8, 2016
  9. 4/8 write_file: drop "gently" formJeff King, Jul 8, 2016
  10. 5/8 write_file: use xopenJeff King, Jul 8, 2016
  11. 6/8 write_file: add pointer+len variantJeff King, Jul 8, 2016
  12. 7/8 write_file: add format attributeJeff King, Jul 8, 2016
  13. Jeff KingJul 8, 2016
  14. 1/2 walker: let walker_say take arbitrary formatsJeff King, Jul 8, 2016
  15. 2/2 avoid using sha1_to_hex output as printf formatJeff King, Jul 8, 2016
  16. Jeff KingJul 8, 2016
  17. Junio C HamanoJul 8, 2016
  18. Junio C HamanoJul 8, 2016
  19. Jeff KingJul 8, 2016
  20. 8/8 use write_file_buf where applicableJeff King, Jul 8, 2016
  21. 9/8 branch: use write_file_buf instead of write_fileJeff King, Jul 8, 2016
  22. René ScharfeJul 8, 2016
  23. Johannes SchindelinJul 9, 2016
  24. Johannes SchindelinJul 10, 2016
  25. Johannes SchindelinJul 8, 2016
  26. René ScharfeJul 8, 2016
  27. Jeff KingJul 8, 2016

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.