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
PWPhillip Wood <phillip.wood123@gmail.com>
Date
May 9, 2025, 13:08 UTC
Message-ID
<ac89ba0a-b50e-469c-99ce-c70b825ae4af@crinan.ddns.net>
In-Reply-To
<aB2ejA1tCK9DR1Nq@pks.im>
On 09/05/2025 07:19, Patrick Steinhardt wrote:
> 
> All of that starts to become kind of involved though. So unless others
> disagree with my analysis I just don't think this edge case really is
> worth the complexity.

I agree with this. We seem to be copying the string because argv is a const char** but would it really be so bad to modify the string that gets passed to us? We know that any bundled options that come before an unknown option cannot have a value or else what we see as the unknown option would be part of that value. So I think we could do

diff --git a/parse-options.c b/parse-options.c
index 35fbb3b0d6..9e6e46da27 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -924,12 +924,12 @@ enum parse_opt_result parse_options_step(struct parse_opt_ctx_t *ctx,
                                          if (internal_help && *ctx->opt == 'h')
                                                  goto show_usage;

-                                        /* fake a short option thing to hide the fact that we may have
+                                        /* move a short option thing to hide the fact that we may have
                                           * started to parse aggregated stuff
-                                         *
-                                         * This is leaky, too bad.
                                           */
-                                        ctx->argv[0] = xstrdup(ctx->opt - 1);
+                                        MOVE_ARRAY((char *)arg, ctx->opt - 1,
+                                                   strlen(ctx->opt) + 2);
+                                        ctx->argv[0] = arg;
                                          *(char *)ctx->argv[0] = '-';
                                          goto unknown;
                                  case PARSE_OPT_NON_OPTION:

Best Wishes

Phillip
Previous: lidongyanNext: lidongyan
Message 12 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.