]> git.ozlabs.org Git - ccan/blobdiff - ccan/mem/mem.c
mem: get clever with memeqzero().
[ccan] / ccan / mem / mem.c
index af41f9e011edbc6738667c3fc89d9d043b1dbcf6..5eb15070ed3c3f9f5bf8d2dd8b0d0f060ffa1766 100644 (file)
@@ -92,13 +92,18 @@ void memswap(void *a, void *b, size_t n)
 bool memeqzero(const void *data, size_t length)
 {
        const unsigned char *p = data;
-       static unsigned long zeroes[16];
+       size_t len;
 
-       while (length > sizeof(zeroes)) {
-               if (memcmp(zeroes, p, sizeof(zeroes)))
+       /* Check first 16 bytes manually */
+       for (len = 0; len < 16; len++) {
+               if (!length)
+                       return true;
+               if (*p)
                        return false;
-               p += sizeof(zeroes);
-               length -= sizeof(zeroes);
+               p++;
+               length--;
        }
-       return memcmp(zeroes, p, length) == 0;
+
+       /* Now we know that's zero, memcmp with self. */
+       return memcmp(data, p, length) == 0;
 }