Re: [PATCH v5 16/16] reftable: be more paranoid about 0-length memcpy calls
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 24, 2021, 04:16 UTC
- Message-ID
- <xmqqy24a4oyq.fsf@gitster.g>
- In-Reply-To
- <CAFQ2z_OXPw9-hLsaD+E1nGESS7eJH5o5sRrr_R_vW1Oa-vvZoA@mail.gmail.com>
Han-Wen Nienhuys <hanwen@google.com> writes:
Show 18 quoted lines
> On Wed, Dec 22, 2021 at 11:50 PM Junio C Hamano <gitster@pobox.com> wrote:
>
>> > - memcpy(r->hash_prefix, key.buf, key.len);
>> > + if (key.len)
>> > + memcpy(r->hash_prefix, key.buf, key.len);
>> > r->hash_prefix_len = key.len;
>> >
>> > if (val_type == 0) {
>>
>> I am not sure why any of these are needed.
>
> I'm not sure they are needed, but IMO it's not worth spending brain
> cycles on deciding either way. Checking the length is always a safe
> alternative.
>
> I would support having a safe_memcpy() that does this check centrally
> (note how our array_copy() array function also does this check, even
> if it's not always needed.)But your safe_memcpy() should not be
safe_memcpy(dst, src, n)
{
if (n)
memcpy(dst, src, n);
return dst;
}Using memcpy() with size==0 is not a crime wrt the language. Passing an invalid pointer while doing so is.
It should rather be
safe_memcpy(dst, src, n)
{
if (dst)
memcpy(dst, src, n);
else if (n)
BUG(...);
return dst;
}to support a common pattern of <ptr, len> that lazily allocates the ptr only when len goes more than zero from triggering an error from picky runtime, compiler and tools. We want to allow "dst==NULL && n==0", while still catching "dst==NULL && n!=0" as an error.
And these places, I do not think use of safe_memcpy() is relevant.