From: lidongyan <502024330056@smail.nju.edu.cn> Date: Fri, 09 May 2025 07:35:18 GMT Subject: Re: [PATCH v2] parse-options: fix xstrdup leak in parse_options_step parse-options:984 Message-ID: In-Reply-To: 2025年5月9日 14:19,Patrick Steinhardt 写道: > 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.