[PATCH v2 3/4] Say malloc (0) != NULL is now common; resection

Paul Eggert eggert@cs.ucla.edu
Thu Feb 12 18:19:44 GMT 2026


On 2/11/26 14:37, DJ Delorie wrote:
> Paul Eggert <eggert@cs.ucla.edu> writes:
>> +However, in IBM AIX a successful @code{malloc (0)} returns a null pointer,
>> +and this breaks common code such as the implementation of @code{xmalloc}
>> +given in this manual (@pxref{Malloc Examples}).
> 
> Do we need to mention AIX by name?  Can't we just say ", in some
> implementations, malloc(0) may return a null pointer."

We could say that. However, AIX is the only extant system that does it. 
I'll reword to say, "However, IBM AIX is unusual in that a successful ...".

> We should add a note about how to be portable (that's the purpose of
> this section, yes? ;) like "Thus, portable programs should never pass a
> size of zero to allocation functions."

I wouldn't go that far, as it's portable to pass 0 to malloc, so long as 
you're willing to deal with the resulting null pointer. Instead, we can 
say that passing non-zero is one way to address the issue, and 
tolerating a null return pointer is another way. I'll add proposed 
wording along those lines.

>> +@item
>> +In other implementations, a non-null pointer returned
> 
> "In some non-GNU implementations,"

Thanks.

>> +In @theglibc{}, the vector is aligned the same way that @code{malloc}
>> +aligns its returned values.  In other implementations, it is merely
> 
> s/it is/it may be/

Thanks.

> 
>> @@ -1927,7 +1973,9 @@ functions (that is, all the functions used by the application,
>>   failures, and, at run time, to heap corruption and application crashes.
>>   Replacement functions should implement the behavior documented for
>>   their counterparts in @theglibc{}; for example, the replacement
>> -@code{free} should also preserve @code{errno}.
>> +@code{malloc} should return a null pointer only when failing
>> +and should return pointers aligned to @code{alignof (max_align_t)},
>> +and the replacement @code{free} should preserve @code{errno}.
> 
> Too many "and"'s.  Replace the first with a comma?  Or with ", it" ?

Thanks, the latter sounds good.



More information about the Libc-alpha mailing list