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

Re: [PATCH v10 4/6] notes.c: introduce '[--[no-]separator|--separator=<paragraph-break>]' option

From
Teng Long <dyroneteng@gmail.com>
Date
May 27, 2023, 07:17 UTC
Message-ID
<20230527071721.24285-1-tenglong.tl@alibaba-inc.com>
In-Reply-To
<20230519005447.GA2955320@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 9 quoted lines
> > +static void insert_separator(struct strbuf *message, size_t pos)
> > +{
> > +	if (!separator)
> > +		return;
> > +	else if (separator[strlen(separator) - 1] == '\n')
> > +		strbuf_insertstr(message, pos, separator);
> > +	else
> > +		strbuf_insertf(message, pos, "%s%s", separator, "\n");
> > +}
Show 8 quoted lines
> This function causes UBSan to complain on 'next' (though curiously only
> with clang, not with gcc[1]). The version in next seems to be from your
> v9, but it's largely the same except for the "if (!separator)"
> condition.
> 
> The problem is in the middle condition here. If "separator" is non-NULL,
> but is an empty string, then strlen() will return 0, and we will look at
> the out-of-bounds byte just before the string.
You definitely correct, will fix.
Show 28 quoted lines
> This function causes UBSan to complain on 'next' (though curiously only
> with clang, not with gcc[1]). The version in next seems to be from your
> v9, but it's largely the same except for the "if (!separator)"
> condition.
> 
> The problem is in the middle condition here. If "separator" is non-NULL,
> but is an empty string, then strlen() will return 0, and we will look at
> the out-of-bounds byte just before the string.
> 
> We'd probably want something like this:
> 
> diff --git a/builtin/notes.c b/builtin/notes.c
> index 3215bce19b..a46d6dac5c 100644
> --- a/builtin/notes.c
> +++ b/builtin/notes.c
> @@ -231,7 +231,8 @@ static void write_note_data(struct note_data *d, struct object_id *oid)
>  
>  static void insert_separator(struct strbuf *message, size_t pos)
>  {
> -	if (separator[strlen(separator) - 1] == '\n')
> +	size_t sep_len = strlen(separator);
> +	if (sep_len && separator[sep_len - 1] == '\n')
>  		strbuf_addstr(message, separator);
>  	else
>  		strbuf_insertf(message, pos, "%s%s", separator, "\n");
> 
> to fix it, though I am not 100% clear on what is supposed to happen for
> an empty separator here.

It's supposed to be the same behaviour with not to specify the option, which is the default behaviour(to use a '\n' as the separator).

The diff looks good to me, will apply.
Show 7 quoted lines
> I was also confused that applying the fix on top of the culprit in
> 'next', 3993a53a13 (notes.c: introduce '--separator=<paragraph-break>'
> option, 2023-04-28), still leads to test failures in t3301. But I think
> that is independent of this fix. It fails even without my patch above
> (and without UBSan) in test 66, "append: specify separator with line
> break". But the failure goes away in the following patch, ad3d1f8feb
> (notes.c: append separator instead of insert by pos, 2023-04-28).

Yes, that's a problem which be taken in patch v9 4/6[1] at insert_separator(...) , we should use strbuf_insert* api here, otherwise will always do append but not to do insert with the position, finally break the test.

In the v9 5/6 patch[2], I tried to remove the postion to simply the logic from insert with position to just append, and this patch cover the test case failure in 4/6.

Show 6 quoted lines
> I haven't been following this series enough to know what's going on, but
> you may want to figure out where the failure is coming from in
> 3993a53a13. If the change in ad3d1f8feb is merely papering over it, then
> we'd need to find and fix the true cause. If the bug is really fixed by
> ad3d1f8feb, we might want to squash those two together to avoid broken
> bisections.
Sure, we should avoid that, will fix.
Show 7 quoted lines
> [1] To reproduce, I did:
> 
>       git checkout 3993a53a13
>       make SANITIZE=address,undefined CC=clang
>       cd t && ./t3301-notes.sh -v -i
> 
>     I'm using clang-14 on a Debian machine.

Do you always do the 'make' with 'SANITIZE=address,undefined', should I follow that approach, may I ask you to give some advices about it?

Thanks.

[1] https://public-inbox.org/git/ed930ef4f795f30792bc14d9c1939484e4976db8.1682671758.git.dyroneteng@gmail.com/ [2] https://public-inbox.org/git/eea2246f44a3adfc4888db93975854448271032b.1682671758.git.dyroneteng@gmail.com/

Previous: Jeff KingNext: Jeff King
Message 38 of 55 in “notes.c: introduce "--separator" option”
  1. 0/6 notes.c: introduce "--separator" optionTeng Long, Apr 28, 2023
  2. 1/6 notes.c: cleanup 'strbuf_grow' call in 'append_edit'Teng Long, Apr 28, 2023
  3. 2/6 notes.c: use designated initializers for clarityTeng Long, Apr 28, 2023
  4. 3/6 t3321: add test cases about the notes stripspace behaviorTeng Long, Apr 28, 2023
  5. 4/6 notes.c: introduce '--separator=<paragraph-break>' optionTeng Long, Apr 28, 2023
  6. Junio C HamanoApr 28, 2023
  7. Teng LongMay 6, 2023
  8. Teng LongMay 6, 2023
  9. Kristoffer HaugsbakkMay 10, 2023
  10. Teng LongMay 12, 2023
  11. Kristoffer HaugsbakkMay 12, 2023
  12. Junio C HamanoMay 16, 2023
  13. Teng LongMay 17, 2023
  14. Junio C HamanoMay 17, 2023
  15. Junio C HamanoJun 14, 2023
  16. notes: do not access before the beginning of an arrayJunio C Hamano, Jun 14, 2023
  17. Eric SunshineJun 14, 2023
  18. Junio C HamanoJun 14, 2023
  19. Jeff KingJun 15, 2023
  20. Junio C HamanoJun 15, 2023
  21. Teng LongJun 19, 2023
  22. Junio C HamanoJun 20, 2023
  23. Teng LongJun 21, 2023
  24. 5/6 notes.c: append separator instead of insert by posTeng Long, Apr 28, 2023
  25. 6/6 notes.c: introduce "--[no-]stripspace" optionTeng Long, Apr 28, 2023
  26. Junio C HamanoApr 28, 2023
  27. Junio C HamanoMay 1, 2023
  28. 0/6 notes.c: introduce "--separator" optionTeng Long, May 18, 2023
  29. 1/6 notes.c: cleanup 'strbuf_grow' call in 'append_edit'Teng Long, May 18, 2023
  30. 2/6 notes.c: use designated initializers for clarityTeng Long, May 18, 2023
  31. 3/6 t3321: add test cases about the notes stripspace behaviorTeng Long, May 18, 2023
  32. 5/6 notes.c: append separator instead of insert by posTeng Long, May 18, 2023
  33. 4/6 notes.c: introduce '[--[no-]separator|--separator=<paragraph-break>]' optionTeng Long, May 18, 2023
  34. Kristoffer HaugsbakkMay 18, 2023
  35. Teng LongMay 20, 2023
  36. Kristoffer HaugsbakkMay 20, 2023
  37. Jeff KingMay 19, 2023
  38. Teng LongMay 27, 2023
  39. Jeff KingMay 27, 2023
  40. Teng LongMay 29, 2023
  41. 6/6 notes.c: introduce "--[no-]stripspace" optionTeng Long, May 18, 2023
  42. Kristoffer HaugsbakkMay 18, 2023
  43. Teng LongMay 20, 2023
  44. Junio C HamanoMay 18, 2023
  45. Teng LongMay 20, 2023
  46. 0/7 notes.c: introduce "--separator"Teng Long, May 27, 2023
  47. 1/7 notes.c: cleanup 'strbuf_grow' call in 'append_edit'Teng Long, May 27, 2023
  48. 2/7 notes.c: use designated initializers for clarityTeng Long, May 27, 2023
  49. 3/7 t3321: add test cases about the notes stripspace behaviorTeng Long, May 27, 2023
  50. 4/7 notes.c: introduce '--separator=<paragraph-break>' optionTeng Long, May 27, 2023
  51. 5/7 notes.c: append separator instead of insert by posTeng Long, May 27, 2023
  52. 6/7 notes.c: introduce "--[no-]stripspace" optionTeng Long, May 27, 2023
  53. 7/7 notes: introduce "--no-separator" optionTeng Long, May 27, 2023
  54. Junio C HamanoJun 1, 2023
  55. Teng LongJun 3, 2023

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.