[PATCH] D31295: Add free_on_realloc_zero=true flag for compatibility with allocators which allow a realloc(p, 0) and don't free the pointer.

Filipe Cabecinhas via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 23 14:49:19 PDT 2017


On Thu, Mar 23, 2017 at 8:45 PM, Kostya Serebryany via Phabricator
<reviews at reviews.llvm.org> wrote:
> kcc added a comment.
>
>> The standard says the behaviour is implementation defined, IIRC (I can quote later).
>
> Yes, please!

>From http://www.open-std.org/jtc1/sc22/wg14/www/standards.html
"The latest publicly available version of the C99 standard is the
combined C99 + TC1 + TC2 + TC3, WG14 N1256, dated 2007-09-07. This is
a WG14 working paper, but it reflects the consolidated standard at the
time of issue."

N1256, Annex J (Portability issues)
J.1 Unspecified behavior
...
— The amount of storage allocated by a successful call to the calloc, malloc, or
realloc function when 0 bytes was requested (7.20.3).
...

J.3 Implementation-defined behavior
1 A conforming implementation is required to document its choice of
behavior in each of
the areas listed in this subclause. The following are implementation-defined:
...
J3.12 Library Functions
...
— Whether the calloc, malloc, and realloc functions return a null pointer or a
pointer to an allocated object when the size requested is zero (7.20.3).
...


POSIX does seem to mandate the "realloc(p, 0) ~= free(p)", and I see
logic in that. But the C standard is not as strong and some
implementations disagree.


Thank you,
 Filip

>> I can keep this patch private if it's too much of a problem.
>
> It's not too much of a problem, and I *am* interested to have reasonable patches upstream.
> Let's see what the standard says.
>
>
> https://reviews.llvm.org/D31295
>
>
>


More information about the llvm-commits mailing list