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

Re: [PATCH] refs.c: add a function to append a reflog entry to a fd

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 20, 2014, 01:34 UTC
Message-ID
<xmqqvbma4pu3.fsf@gitster.dls.corp.google.com>
In-Reply-To
<xmqqzjbm4qe5.fsf@gitster.dls.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 12 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> Compared to the last send of this patch[1], there was one change in the print
>> function. Replaced sprintf by snprintf for security reasons. 
>
> Careful.  I despise people who blindly think strlcpy() and
> snprintf() are good solutions for for security.  They are by
> themselves not.
> ...
> So use of snprintf() is not really buying you much here, not in the
> current code certainly, but not as a future-proofing measure,
> either.

Don't get me wrong. I am not saying that using snprintf() here is bad per-se. There should be no difference.

But I do not want to see people getting in the habit of thinking "I now use snprintf/strlcpy instead of sprintf/strcpy, and made my code more secure." Often they are not doing that.

The only case snprintf/strlcpy is useful is when your data does not matter in its detail. E.g. when you are preparing human-readble data whose first part is sufficient to convey the information you want to convey, you would be perfectly happy if the result is truncated. In such a case, counting to allocate enough to hold everything and running sprintf() only to chop the result later is not necessary --- it still is not wrong, though --- and allocating enough to satisify the eventual chop length and using snprintf() is easier way to achieve the same result.

But I do not think this codepath falls into such an "I am willing to lose data" case.

Previous: Junio C HamanoNext: Stefan Beller
Message 3 of 18 in “refs.c: add a function to append a reflog entry to a fd”
  1. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  2. Junio C HamanoNov 20, 2014
  3. Junio C HamanoNov 20, 2014
  4. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  5. Jonathan NiederNov 20, 2014
  6. Junio C HamanoNov 20, 2014
  7. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  8. Jonathan NiederNov 20, 2014
  9. Stefan BellerNov 20, 2014
  10. Jonathan NiederNov 20, 2014
  11. Junio C HamanoNov 20, 2014
  12. Stefan BellerNov 20, 2014
  13. Jonathan NiederNov 20, 2014
  14. Junio C HamanoNov 20, 2014
  15. refs.c: add a function to append a reflog entry to a fdStefan Beller, Nov 20, 2014
  16. Jonathan NiederNov 20, 2014
  17. Junio C HamanoNov 20, 2014
  18. Jonathan NiederNov 20, 2014

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.