[PATCH v2] Skip the null pointer check before calling realloc

Alex Henrie alexhenrie24 at gmail.com
Wed Nov 18 19:05:20 UTC 2015


2015-11-18 6:42 GMT-07:00 Dmitry Samersoff <dmitry.samersoff at oracle.com>:
> Please notice that
>
>> + segments = (char**)realloc( segments, (count+1)*sizeof(char*) );
>
> would leak a memory if realloc fails (original code also has this problem).
>
> So if jplis_assert doesn't abort entire application, it's better to write:
>
> new_segments = (char**) realloc(segments, (count + 1) * sizeof(char*));
> if (new_segments == NULL) {
>    free(segments);
>    goto OOM;
> }
>
> segments = new_segments;

Interesting point. However, if the realloc fails, segments will be
NULL and the program will immediately crash because of the
"segments[count++] = c;" line. I'm not inclined to worry about it.

Regardless, this patch has to be resubmitted to the serviceability-dev
mailing list, so we'll see if the developers there have any more
comments.

-Alex


More information about the jdk9-dev mailing list