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

Re: [PATCH v2 3/7] trace2: remove use of xstrdup()

From
Derrick Stolee <stolee@gmail.com>
Date
Aug 31, 2026, 12:41 UTC
Message-ID
<2eadc838-9d47-442d-a94a-efc570624489@gmail.com>
In-Reply-To
<CABPp-BH1TeDTeqddZw+cvzou+3PRgw+HNpYF2JnhMTSBp9qfbQ@mail.gmail.com>
On 8/25/2026 6:14 PM, Elijah Newren wrote:
Show 40 quoted lines
> On Tue, Aug 25, 2026 at 11:58 AM Derrick Stolee via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>>
> [...]
>> For full defense in depth, we remove the xstrdup() calls from
>> trace2/tr2_sysenv.c.
>>
>> First, in tr2_sysenv_cb(), we need to handle a failed assignment of the
>> value with a negative return to halt the config parsing loop.
>>
> [...]
>> --- a/trace2/tr2_sysenv.c
>> +++ b/trace2/tr2_sysenv.c
>> @@ -74,7 +74,9 @@ static int tr2_sysenv_cb(const char *key, const char *value,
>>                         if (!value)
>>                                 return config_error_nonbool(key);
>>                         free(tr2_sysenv_settings[k].value);
>> -                       tr2_sysenv_settings[k].value = xstrdup(value);
>> +                       tr2_sysenv_settings[k].value = strdup(value);
>> +                       if (!tr2_sysenv_settings[k].value)
>> +                               return -1;
> 
> I'm not sure if this matters, but I think the call sequence from
> config.c to this function is:
> 
>   read_very_early_config ->
>     config_with_options ->
>       git_config_from_file_with_options ->
>         do_config_from_file ->
>           do_config_from ->
>             git_parse_source ->
>               get_value ->
>                 git_config_include ->
>                   tr2_sysenv_cb
> 
> and the -1 unwinds back to git_parse_source, which breaks, formats an
> error message, and calls die:
> 
>    error_msg = xstrfmt(_("bad config line %d in file %s")...)
>    die("%s", error_msg)

Thanks for the careful read! It's particularly important that we don't suggest that the config value is bad because we couldn't allocate memory.

Show 5 quoted lines
> Am I reading this right?  If so, the -1 actually triggers a die as
> well -- unless the allocation in xstrfmt manages to kill it first.
> This isn't a regression (the old xstrdup() also died) and the die
> isn't inside the trace functions, but the commit message might read as
> promising more than it delivers.

Yes, I believe you are correct. We should return 0 to terminate early without a failure.

That said, I think that the die() in the config code will remain a "safe" place to die(), as we won't re-trigger this config-parsing code during any tracing of that die() message. But it's best to be safe and have the tracing continue to be "best effort" when system calls fail.

Thanks, -Stolee

Previous: Elijah NewrenNext: Derrick Stolee via GitGitGadget
Message 21 of 43 in “trace2: tolerate failed timestamp formatting”
  1. trace2: tolerate failed timestamp formattingDerrick Stolee via GitGitGadget, Jul 15, 2026
  2. Taylor BlauJul 17, 2026
  3. Derrick StoleeJul 18, 2026
  4. Junio C HamanoJul 20, 2026
  5. Taylor BlauJul 20, 2026
  6. Junio C HamanoJul 29, 2026
  7. Derrick StoleeJul 31, 2026
  8. Junio C HamanoJul 31, 2026
  9. 0/7 trace2: stop allowing die()Derrick Stolee via GitGitGadget, Aug 25, 2026
  10. 1/7 banned-die: create header for banning of functionsDerrick Stolee via GitGitGadget, Aug 25, 2026
  11. Junio C HamanoAug 25, 2026
  12. Derrick StoleeAug 31, 2026
  13. Patrick SteinhardtAug 31, 2026
  14. Elijah NewrenAug 25, 2026
  15. Derrick StoleeAug 31, 2026
  16. Jeff KingAug 27, 2026
  17. Derrick StoleeAug 31, 2026
  18. 2/7 trace2: tolerate failed timestamp formattingDerrick Stolee via GitGitGadget, Aug 25, 2026
  19. 3/7 trace2: remove use of xstrdup()Derrick Stolee via GitGitGadget, Aug 25, 2026
  20. Elijah NewrenAug 25, 2026
  21. Derrick StoleeAug 31, 2026
  22. 4/7 trace2: remove use of ALLOC_ARRAY()Derrick Stolee via GitGitGadget, Aug 25, 2026
  23. 5/7 trace2: remove use of xstrfmt()Derrick Stolee via GitGitGadget, Aug 25, 2026
  24. Elijah NewrenAug 25, 2026
  25. Junio C HamanoAug 25, 2026
  26. Derrick StoleeAug 31, 2026
  27. 6/7 trace2: remove use of ALLOC_GROW()Derrick Stolee via GitGitGadget, Aug 25, 2026
  28. Elijah NewrenAug 25, 2026
  29. 7/7 trace2: remove use of xcalloc()Derrick Stolee via GitGitGadget, Aug 25, 2026
  30. Jeff KingAug 27, 2026
  31. Derrick StoleeAug 31, 2026
  32. Jeff KingSep 1, 2026
  33. Jeff KingSep 1, 2026
  34. Derrick StoleeSep 1, 2026
  35. 0/7 trace2: stop allowing die()Derrick Stolee via GitGitGadget, Aug 31, 2026
  36. 1/7 banned-die: create header for banning of functionsDerrick Stolee via GitGitGadget, Aug 31, 2026
  37. 2/7 trace2: tolerate failed timestamp formattingDerrick Stolee via GitGitGadget, Aug 31, 2026
  38. 3/7 trace2: remove use of xstrdup()Derrick Stolee via GitGitGadget, Aug 31, 2026
  39. 4/7 trace2: remove use of ALLOC_ARRAY()Derrick Stolee via GitGitGadget, Aug 31, 2026
  40. 5/7 trace2: remove use of xstrfmt()Derrick Stolee via GitGitGadget, Aug 31, 2026
  41. 6/7 trace2: remove use of ALLOC_GROW()Derrick Stolee via GitGitGadget, Aug 31, 2026
  42. 7/7 trace2: remove use of xcalloc()Derrick Stolee via GitGitGadget, Aug 31, 2026
  43. Derrick StoleeOct 6, 2026

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.