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

Re: [PATCH v2] parse-options: fix xstrdup leak in parse_options_step parse-options:984

From
lidongyan <502024330056@smail.nju.edu.cn>
Date
May 9, 2025, 07:35 UTC
Message-ID
<CC653689-3047-4DFD-9893-46D08F948C10@smail.nju.edu.cn>
In-Reply-To
<aB2ejA1tCK9DR1Nq@pks.im>
2025年5月9日 14:19,Patrick Steinhardt <ps@pks.im> 写道:
> So does that mean that _every_ user of the "parse-options" interfaces
> now has to explicitly plug this memory leak when facing unknown options?
> That sounds rather undesirable, as there are so many users out there.

Since the lifetime of `argv` last until the program terminates, a memory leak can only occur if parse_option is called multiple times and at least two of those calls use the `PARSE_OPT_KEEP_UNKNOWN` flag. In the other words, the memory leak only occurs when the statement `ctx->argue[0] = xstrdup` overwrites the result of a previous `xstrdup` call.

> Hm. Is there any other usecase for the `strdup_fn` field that you can
> think about in the future? Otherwise it feels a bit overengineered from
> my perspective.

I think a simple approach is to add a marker to the string allocated by `xstrdup` , and before the next potential leaking `ctx->argv[0] = xstrdup`, check whether the string needs to be freed. Like we could allocate one more byte in the end of the string to store the marker.

Previous: Patrick SteinhardtNext: Phillip Wood
Message 11 of 14 in “fix xstrdup leak in parse_short_opt”
  1. 0/3 fix xstrdup leak in parse_short_optLidong Yan via GitGitGadget, May 7, 2025
  2. 1/3 fix xstrdup leak in parse_short_optLidong Yan via GitGitGadget, May 7, 2025
  3. Patrick SteinhardtMay 7, 2025
  4. 2/3 fix: replace bug where int was incorrectly used as boolLidong Yan via GitGitGadget, May 7, 2025
  5. Patrick SteinhardtMay 7, 2025
  6. 3/3 fix: use strvec_push_wrapper to prevent ubsan failureLidong Yan via GitGitGadget, May 7, 2025
  7. Patrick SteinhardtMay 7, 2025
  8. lidongyanMay 7, 2025
  9. parse-options: fix xstrdup leak in parse_options_step parse-options:984Lidong Yan via GitGitGadget, May 7, 2025
  10. Patrick SteinhardtMay 9, 2025
  11. lidongyanMay 9, 2025
  12. Phillip WoodMay 9, 2025
  13. lidongyanMay 9, 2025
  14. parse-options: fix memory leak when calling `parse_options`Lidong Yan via GitGitGadget, May 9, 2025

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.