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

Re: [PATCH] use xstrdup, not strdup in ll-merge.c

From
Jim Meyering <jim@meyering.net>
Date
Jun 15, 2009, 08:02 UTC
Message-ID
<87fxe2lybr.fsf@meyering.net>
In-Reply-To
<81b0412b0906141503v14484d9fyea56198910305bfc@mail.gmail.com>
Alex Riesen wrote:
Show 9 quoted lines
> 2009/6/14 Jim Meyering <jim@meyering.net>:
>> @@ -231,7 +231,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
>>
>>        if (!strcmp(var, "merge.default")) {
>>                if (value)
>> -                       default_ll_merge = strdup(value);
>> +                       default_ll_merge = xstrdup(value);
>
> read_merge_config has a failure mode (where it returns -1), why not use it?

I didn't even consider it, because it would be inconsistent with the other heap-allocation functions used there (xcalloc, xmemdupz).

However, now that I do, it looks like that would mean adding four times the same code (including conditionals and code to generate a diagnostic via a call to error -- or a goto). Why bother, when all of that is already encapsulated in xmalloc? Maybe because you want to be able to continue after an allocation failure? If a small strdup allocation fails, odds are good that the code won't be able to do anything useful, so when not in library code, cleanest is simply to exit.

In addition, if you insist on using strdup, you'll probably want to be consistent and use calloc and memdupz, too. Adding all of the code required to recover from those failures and to avoid leaks would be messy.

Previous: Alex RiesenNext: Alex Riesen
Message 3 of 10 in “use xstrdup, not strdup in ll-merge.c”
  1. use xstrdup, not strdup in ll-merge.cJim Meyering, Jun 14, 2009
  2. Alex RiesenJun 14, 2009
  3. Jim MeyeringJun 15, 2009
  4. Alex RiesenJun 15, 2009
  5. Jim MeyeringJun 15, 2009
  6. Alex RiesenJun 15, 2009
  7. Jim MeyeringJun 15, 2009
  8. Alex RiesenJun 15, 2009
  9. Shawn O. PearceJun 15, 2009
  10. Alex RiesenJun 15, 2009

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.