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

Re: [PATCH] mailinfo: use starts_with() when checking scissors

From
Jeff King <peff@peff.net>
Date
Jun 8, 2021, 21:57 UTC
Message-ID
<YL/n1b641m+wuVHG@coredump.intra.peff.net>
In-Reply-To
<20210608204841.2793591-1-rybak.a.v@gmail.com>
On Tue, Jun 08, 2021 at 10:48:41PM +0200, Andrei Rybak wrote:
Show 19 quoted lines
> Existing checks for scissors characters using memcmp(3) never read past
> the end of the line, because all substrings we are interested in are two
> characters long, and the outer loop guarantees we have at least one
> character.  So at most we will look at the NUL.
> 
> However, this is too subtle and may lead to bugs in code which copies
> this behavior without realizing substring length requirement.  So use
> starts_with() instead, which will stop at NUL regardless of the length
> of the prefix.  Remove extra pair of parentheses while we are here.
> 
> Helped-by: Jeff King <peff@peff.net>
> Signed-off-by: Andrei Rybak <rybak.a.v@gmail.com>
> ---
>  mailinfo.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> This patch was originally part of:
> https://public-inbox.org/git/20190401215334.18678-1-rybak.a.v@gmail.com/
> I've finally gotten around to sending it on its own.
Well, after seeing the date I don't feel too bad for not remembering it. :)

I think this is an improvement in safety and readability, and worth applying. I'd also be fine going further and using skip_prefix(), which would let us drop the magic-number "2", though:

  - as Junio said in that earlier thread, we hope people aren't
    encouraged to add more versions of scissors here anyway).
  - I am not 100% sure that the increment of "c" in the conditional
    is not also relying on that "2", as well (i.e., it is incrementing
    one, and then the for-loop increments one). There's a non-trivial
    risk of introducing off-by-one errors or even more subtlety here. :)
So I'm OK leaving that. Thanks for resurrecting this cleanup.
-Peff
Previous: Andrei RybakNext: Junio C Hamano
Message 13 of 16 in “mailinfo: support Unicode scissors”
  1. mailinfo: support Unicode scissorsAndrei Rybak, Mar 31, 2019
  2. SZEDER GáborMar 31, 2019
  3. Junio C HamanoApr 1, 2019
  4. Jeff KingApr 1, 2019
  5. Duy NguyenApr 1, 2019
  6. Andrei RybakApr 1, 2019
  7. 1/2 mailinfo: use starts_with() for clarityAndrei Rybak, Apr 1, 2019
  8. 2/2 mailinfo: support Unicode scissorsAndrei Rybak, Apr 1, 2019
  9. Jeff KingApr 2, 2019
  10. Junio C HamanoApr 3, 2019
  11. Jeff KingApr 2, 2019
  12. mailinfo: use starts_with() when checking scissorsAndrei Rybak, Jun 8, 2021
  13. Jeff KingJun 8, 2021
  14. Junio C HamanoJun 9, 2021
  15. Jeff KingJun 9, 2021
  16. Junio C HamanoJun 9, 2021

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.