From 07c42492e58635b9064f68234e5cfa6e8b1b48ca Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 16 Nov 2015 23:17:42 +0100 Subject: [PATCH] Repair sodium_is_zero() --- src/libsodium/sodium/utils.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index fd629e13..334b0c00 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -158,18 +158,23 @@ int sodium_is_zero(const unsigned char *n, const size_t nlen) { size_t i = 0U; + #if !defined(CPU_UNALIGNED_ACCESS) || !defined(NATIVE_LITTLE_ENDIAN) - uint_fast8_t c = 0U; -#else - uint_fast64_t c = 0U; - for (; i < (nlen & ~0x7); i += 8U) { - c |= *((const uint64_t *) (const void *) &n[i]); - } -#endif + uint_fast8_t c = 0U; for (; i < nlen; i++) { c |= n[i]; } - return (int) (((c - 1U) >> 8) & 1U); + return (int) ((((uint_fast16_t) c - 1U) >> 8) & 1U); +#else + uint_fast32_t c = 0U; + for (; i < (nlen & ~0x3); i += 4U) { + c |= *((const uint32_t *) (const void *) &n[i]); + } + for (; i < nlen; i++) { + c |= n[i]; + } + return (int) ((((uint_fast64_t) c - 1U) >> 32) & 1U); +#endif } void