On 09/30/2010 10:08 AM, Nikola Pajkovsky wrote:
On 09/30/2010 09:37 AM, Karel Klic wrote:
> Dne 29.9.2010 16:34, Jiri Moskovcak napsal(a):
>> On 09/29/2010 04:29 PM, Nikola Pajkovsky wrote:
>>> On 09/29/2010 03:22 PM, Jiri Moskovcak wrote:
>>>> On 09/29/2010 01:17 PM, Nikola Pajkovsky wrote:
>>>>> +void *xcalloc(size_t nmemb, size_t size)
>>>>> +{
>>>>> + void *ptr = calloc(nmemb, size);
>>>>> + if (!ptr&& (!nmemb || !size))
>>>>
>>>> - I think you want if (!ptr&& (!nmemb&&
!size))
>>>>
>>> Nope. NULL may also be returned by a successful call to malloc() with a size
of zero,
>>> or by a successful call to calloc() with nmemb *or* size equal to zero.
>>>
>>
>> exactly and it's not "out of memory" problem
>>
>
> I think it should be "if (!ptr&& nmemb&& size)
die_out_of_memory();"
>
> If the allocation returns NULL, and both nmemb and size are nonzero,
> something is wrong. If nmemb or size is zero, calloc can return NULL
> validly (at least the man page says so).
>
> IMHO it's ok when xcalloc(0, 1) returns NULL, because if you called
> that, you did not intend to touch the pointer anyway (because you wanted
> *zero* members of size 1 and you got them).
>
And this is a problem, calling calloc(0, 1), calloc(0, 0) and calloc(1, 0)
doesn't return NULL, but pointer to zero-length array and you cannot access it,
because *Invalid write of size*
Yes, but the problem is the code accessing the array, not the calloc(0,
0). And if the code asks for 0 length array it should access only zero
items (which means none).
J.
>>>>> + die_out_of_memory();
>>>>> + return ptr;
>>>>> +}
>>>>> +
>
> _______________________________________________
> Crash-catcher mailing list
> Crash-catcher(a)lists.fedorahosted.org
>
https://fedorahosted.org/mailman/listinfo/crash-catcher