Re: [PATCH 06/11] config: use git_config_string() in easy cases
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Apr 7, 2024, 02:05 UTC
- Message-ID
- <CAPig+cST40pM5PdGA==o8aGFwv8kStzzC8=28NULUtV8OFJmLg@mail.gmail.com>
- In-Reply-To
- <20240407010228.GF868358@coredump.intra.peff.net>
On Sat, Apr 6, 2024 at 9:02 PM Jeff King <peff@peff.net> wrote:
> config: use git_config_string() in easy cases
Did you instead mean?
config: use git_config_string_dup() in easy cases
> (like the one above), we can easily convert them to use a variant of the > helper that frees the existing value before writing over it. That works > because free(NULL) is a noop. What we can't change, though, is cases > where the pointer is initialized to a string literal, like:
Fun historical (historic?) fact: On NeXTSTEP operating system, free(NULL) and realloc(NULL,n) would crash the program, so getting non-native (open-source) code to run correctly often involved more than a little effort..
Show 11 quoted lines
> const char *foo = "some string literal"; > > In that case it would be incorrect to free() the original value. Those > cases will take more refactoring to make them leak-free, and this patch > punts on that for now. > > In each case we switch the underlying variable from "const char *" to > "char *" to indicate that it will always point to allocated memory (and > our git_config_string_dup() interface enforces this). > > Signed-off-by: Jeff King <peff@peff.net>