[augeas-devel] [PATCH] check for a few failed memory allocations

Jim Meyering jim at meyering.net
Mon Apr 28 21:34:28 UTC 2008


David Lutterkort <dlutter at redhat.com> wrote:

> On Mon, 2008-04-28 at 19:55 +0200, Jim Meyering wrote:
>> There are may more, but you have to start somewhere...
>>
>> * src/augtool.c: check for a few failed memory allocations
>
> ACK .. thanks for doing that.
>
>> @@ -392,7 +395,10 @@
>>          current += 1;
>>          if (STREQLEN(child, text, strlen(text))) {
>>              if (child_count(child) > 0) {
>> -                child = realloc(child, strlen(child)+2);
>> +                char *c = realloc(child, strlen(child)+2);
>> +                if (c == NULL)
>> +                    return NULL;
>> +                child = c;
>
> Out of curiosity (and for my education): why the additional var 'c' ? I
> understand why you'd do that if you needed to free child after a failed
> allocation, but in this case that's not necessary.

Mainly out of habit, since whenever I see

    x = realloc(x, ...);

it sets off "potential leak" warning bells ;-)

This way doesn't provoke a false positive.
It's better when code doesn't fit the risky-looking pattern
not just for the saved reviewer-time, but eventually (if you
remove all of them), you can use simple regexps to ensure that
there are no such leaks.




More information about the augeas-devel mailing list