[ccan] [PATCH 4/7] crypto/hmac_sha256: Remove undefined memset()

Rusty Russell rusty at rustcorp.com.au
Mon Apr 24 12:30:15 AEST 2017


"Timothy B. Terriberry" <tterribe at xiph.org> writes:
> Rusty Russell wrote:
>>> Well.. more precisely, my understanding is that memset(p, x, 0) can't
>>> be counted on not to dereference p.
>>
>> That seems nonsensical, though.
>
> It is nonsensical, but welcome to standards.
>
> C99 Section 7.21.1 "String function conventions" (which includes memset, 
> as it is in string.h):
>
> "Where an argument declared as size_t n specifies the length of the 
> array for a function, n can have the value zero on a call to that 
> function. Unless explicitly stated otherwise in the description of a 
> particular function in this subclause, pointer arguments on such a call 
> shall still have valid values, as described in 7.1.4"

I think you're confusing "valid pointer" with "dereferencable pointer".

A pointer one past the end is still a valid pointer, just not
dereferencable.  In this case, that's what we have when ksize ==
HMAC_SHA256_BLOCKSIZE:

 	memset((char *)k_ipad + ksize, 0, HMAC_SHA256_BLOCKSIZE - ksize);

ie. we are trying to set the unused trailing bytes of the region to
zero.

Even if you were correct, and code should be testing for zero sizes in
such cases, and don't care that Kernighan and Plauger would be rolling
in their graves (and neither is even dead yet!), you would just cause me
to write a test case.

Because you should *never* give in to insanity except under duress, and
then with loud protest!  Otherwise everyone loses.

Cheers,
Rusty.


More information about the ccan mailing list