utils: Handle NULL consistently if memwipe() is implemented via explicit_bzero()

Our own implementation ignores NULL values, however, explicit_bzero()
can't handle that, as indicated by the `__nonnull ((1))` attribute in the
function's signature in string.h, and causes a segmentation fault.  This
was noticed in one of the unit tests for NewHope.  Since we usually use
memwipe() via chunk_clear(), which already ignores NULL pointers, this
is not that much of an issue in practice.

Fixes: 149d1bbb05 ("memory: Use explicit_bzero() as memwipe() if available")
This commit is contained in:
Tobias Brunner
2019-10-21 13:58:12 +02:00
parent 393e39a1bc
commit cf98706bb8
2 changed files with 70 additions and 1 deletions
+7 -1
View File
@@ -87,7 +87,13 @@ static inline void *memset_noop(void *s, int c, size_t n)
void memxor(uint8_t dest[], const uint8_t src[], size_t n);
#ifdef HAVE_EXPLICIT_BZERO
#define memwipe(ptr, n) explicit_bzero(ptr, n)
static inline void memwipe(void *ptr, size_t n)
{
if (ptr)
{
explicit_bzero(ptr, n);
}
}
#else /* HAVE_EXPLICIT_BZERO */
/**
* Safely overwrite n bytes of memory at ptr with zero, non-inlining variant.