]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Clean up buf == NULL handling on adler32 functions and test strings.
authorNathan Moinvaziri <nathan@nathanm.com>
Mon, 12 Jan 2026 19:18:56 +0000 (11:18 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Tue, 13 Jan 2026 17:43:50 +0000 (18:43 +0100)
18 files changed:
adler32.c
arch/arm/adler32_neon.c
arch/generic/adler32_c.c
arch/loongarch/adler32_lasx.c
arch/loongarch/adler32_lsx.c
arch/power/adler32_power8.c
arch/power/adler32_vmx.c
arch/power/crc32_power8.c
arch/riscv/adler32_rvv.c
arch/x86/adler32_avx2.c
arch/x86/adler32_avx512.c
arch/x86/adler32_avx512_vnni.c
arch/x86/adler32_ssse3.c
crc32.c
test/hash_test_strings_p.h
test/test_adler32_copy.cc
test/test_crc32.cc
test/test_crc32_copy.cc

index 1a643ed53b2c0cd66fb829fc12ff8ce207ec827d..3e3dfca7e449b01d0cf84a37d26aa6bcfae30454 100644 (file)
--- a/adler32.c
+++ b/adler32.c
@@ -9,10 +9,14 @@
 
 #ifdef ZLIB_COMPAT
 unsigned long Z_EXPORT PREFIX(adler32_z)(unsigned long adler, const unsigned char *buf, size_t len) {
+    if (buf == NULL)
+        return ADLER32_INITIAL_VALUE;
     return (unsigned long)FUNCTABLE_CALL(adler32)((uint32_t)adler, buf, len);
 }
 #else
 uint32_t Z_EXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, size_t len) {
+    if (buf == NULL)
+        return ADLER32_INITIAL_VALUE;
     return FUNCTABLE_CALL(adler32)(adler, buf, len);
 }
 #endif
@@ -20,10 +24,14 @@ uint32_t Z_EXPORT PREFIX(adler32_z)(uint32_t adler, const unsigned char *buf, si
 /* ========================================================================= */
 #ifdef ZLIB_COMPAT
 unsigned long Z_EXPORT PREFIX(adler32)(unsigned long adler, const unsigned char *buf, unsigned int len) {
+    if (buf == NULL)
+        return ADLER32_INITIAL_VALUE;
     return (unsigned long)FUNCTABLE_CALL(adler32)((uint32_t)adler, buf, len);
 }
 #else
 uint32_t Z_EXPORT PREFIX(adler32)(uint32_t adler, const unsigned char *buf, uint32_t len) {
+    if (buf == NULL)
+        return ADLER32_INITIAL_VALUE;
     return FUNCTABLE_CALL(adler32)(adler, buf, len);
 }
 #endif
index 4943bb9ffb51cbd03b5fa1ae975390f71a70d65a..0b41e4d2b5d0a051f0defed6b3ba0e98f5f332b2 100644 (file)
@@ -274,10 +274,6 @@ Z_FORCEINLINE static uint32_t adler32_copy_impl(uint32_t adler, uint8_t *dst, co
     if (UNLIKELY(len == 1))
         return adler32_copy_len_1(adler, dst, src, sum2, COPY);
 
-    /* initial Adler-32 value (deferred check for len == 1 speed) */
-    if (UNLIKELY(src == NULL))
-        return 1L;
-
     /* in case short lengths are provided, keep it somewhat fast */
     if (UNLIKELY(len < 16))
         return adler32_copy_len_16(adler, dst, src, len, sum2, COPY);
index d7179da8e69ac1598aaa1cbb8095e19329feaba8..4bd55381689b8cb1635ab4acb2c1977b4ac03607 100644 (file)
@@ -19,10 +19,6 @@ Z_INTERNAL uint32_t adler32_c(uint32_t adler, const uint8_t *buf, size_t len) {
     if (UNLIKELY(len == 1))
         return adler32_copy_len_1(adler, NULL, buf, sum2, 0);
 
-    /* initial Adler-32 value (deferred check for len == 1 speed) */
-    if (UNLIKELY(buf == NULL))
-        return 1L;
-
     /* in case short lengths are provided, keep it somewhat fast */
     if (UNLIKELY(len < 16))
         return adler32_copy_len_16(adler, NULL, buf, len, sum2, 0);
index c63930551cda246555eba33eb7abfd4e01115115..d93f41b0e3e3980f7677512536cfed73966c5f29 100644 (file)
@@ -35,9 +35,6 @@ extern uint32_t adler32_copy_lsx(uint32_t adler, uint8_t *dst, const uint8_t *sr
 extern uint32_t adler32_lsx(uint32_t adler, const uint8_t *src, size_t len);
 
 Z_FORCEINLINE static uint32_t adler32_copy_impl(uint32_t adler, uint8_t *dst, const uint8_t *src, size_t len, const int COPY) {
-    if (UNLIKELY(src == NULL)) return 1L;
-    if (UNLIKELY(len == 0)) return adler;
-
     uint32_t adler0, adler1;
     adler1 = (adler >> 16) & 0xffff;
     adler0 = adler & 0xffff;
index 75b3b4550e9a0aeb695039fdaab7e449cdc4c7ae..8f277b43eaff2ae495d07738bbf2f6e3d433602f 100644 (file)
@@ -30,9 +30,6 @@ static inline uint32_t hsum(__m128i x) {
 }
 
 Z_FORCEINLINE static uint32_t adler32_copy_impl(uint32_t adler, uint8_t *dst, const uint8_t *src, size_t len, const int COPY) {
-    if (UNLIKELY(src == NULL)) return 1L;
-    if (UNLIKELY(len == 0)) return adler;
-
     uint32_t adler0, adler1;
     adler1 = (adler >> 16) & 0xffff;
     adler0 = adler & 0xffff;
index 10338fcc8a680f1ffe07f8568ad1bcf5a8415639..dfa3f7beb15e8759519d82419643bbc5d8de590e 100644 (file)
@@ -60,10 +60,6 @@ Z_FORCEINLINE static uint32_t adler32_impl(uint32_t adler, const uint8_t *buf, s
     if (UNLIKELY(len == 1))
         return adler32_copy_len_1(s1, NULL, buf, s2, 0);
 
-    /* If buffer is empty or len=0 we need to return adler initial value.  */
-    if (UNLIKELY(buf == NULL))
-        return 1;
-
     /* This is faster than VSX code for len < 64.  */
     if (len < 64)
         return adler32_copy_len_64(s1, NULL, buf, len, s2, 0);
index 03a847430ad98f40267c4199da6d6d925d3507b4..e8a6bd71a99a228338f9a0eba3fc4aed545d2991 100644 (file)
@@ -137,10 +137,6 @@ Z_FORCEINLINE static uint32_t adler32_impl(uint32_t adler, const uint8_t *buf, s
     if (UNLIKELY(len == 1))
         return adler32_copy_len_1(adler, NULL, buf, sum2, 0);
 
-    /* initial Adler-32 value (deferred check for len == 1 speed) */
-    if (UNLIKELY(buf == NULL))
-        return 1L;
-
     /* in case short lengths are provided, keep it somewhat fast */
     if (UNLIKELY(len < 16))
         return adler32_copy_len_16(adler, NULL, buf, len, sum2, 0);
index 914cca8ab2c7e28f97d68ef53c0b1ad49099cc7b..05e1170230433d35d177a7eb24e3cf8c96f5b08b 100644 (file)
@@ -52,9 +52,6 @@ Z_INTERNAL uint32_t crc32_power8(uint32_t crc, const unsigned char *p, size_t _l
 
     unsigned long len = (unsigned long) _len;
 
-    if (p == (const unsigned char *) 0x0)
-        return 0;
-
     crc ^= 0xffffffff;
 
     if (len < VMX_ALIGN + VMX_ALIGN_MASK) {
index b9360ed72b6deb4dcb1838f21ad0b7f6a949b2a8..9ae31b30170f2ca471320585634e8c19c0e0eaf6 100644 (file)
@@ -21,10 +21,6 @@ Z_FORCEINLINE static uint32_t adler32_copy_impl(uint32_t adler, uint8_t* restric
     if (UNLIKELY(len == 1))
         return adler32_copy_len_1(adler, dst, src, sum2, COPY);
 
-    /* initial Adler-32 value (deferred check for len == 1 speed) */
-    if (UNLIKELY(src == NULL))
-        return 1L;
-
     /* in case short lengths are provided, keep it somewhat fast */
     if (UNLIKELY(len < 16))
         return adler32_copy_len_16(adler, dst, src, len, sum2, COPY);
index 464e83df05bc0b30003bbbce13e90384e5b5aa61..bf748eb8f31d8df205ef448d783f8e72f5cd5938 100644 (file)
@@ -19,9 +19,6 @@ extern uint32_t adler32_copy_sse42(uint32_t adler, uint8_t *dst, const uint8_t *
 extern uint32_t adler32_ssse3(uint32_t adler, const uint8_t *src, size_t len);
 
 Z_FORCEINLINE static uint32_t adler32_copy_impl(uint32_t adler, uint8_t *dst, const uint8_t *src, size_t len, const int COPY) {
-    if (UNLIKELY(src == NULL)) return 1L;
-    if (UNLIKELY(len == 0)) return adler;
-
     uint32_t adler0, adler1;
     adler1 = (adler >> 16) & 0xffff;
     adler0 = adler & 0xffff;
index 545806bfc53d1f2654e4386c1e9e3dab7942588b..5fffbd360676597b535b7e80eb52443ee82a4027 100644 (file)
@@ -16,9 +16,6 @@
 #include "adler32_avx512_p.h"
 
 Z_FORCEINLINE static uint32_t adler32_copy_impl(uint32_t adler, uint8_t *dst, const uint8_t *src, size_t len, const int COPY) {
-    if (UNLIKELY(src == NULL)) return 1L;
-    if (UNLIKELY(len == 0)) return adler;
-
     uint32_t adler0, adler1;
     adler1 = (adler >> 16) & 0xffff;
     adler0 = adler & 0xffff;
index 9fbb3bf38166f4ff4441606b3e9e8d3d19667228..741176cacd25e723612cdbf581f7057c06debbfc 100644 (file)
@@ -18,9 +18,6 @@
 #include "adler32_avx2_p.h"
 
 Z_INTERNAL uint32_t adler32_avx512_vnni(uint32_t adler, const uint8_t *src, size_t len) {
-    if (UNLIKELY(src == NULL)) return 1L;
-    if (UNLIKELY(len == 0)) return adler;
-
     uint32_t adler0, adler1;
     adler1 = (adler >> 16) & 0xffff;
     adler0 = adler & 0xffff;
@@ -111,9 +108,6 @@ rem_peel:
 
 /* Use 256-bit vectors when copying because 512-bit variant is slower. */
 Z_INTERNAL uint32_t adler32_copy_avx512_vnni(uint32_t adler, uint8_t *dst, const uint8_t *src, size_t len) {
-    if (UNLIKELY(src == NULL)) return 1L;
-    if (UNLIKELY(len == 0)) return adler;
-
     uint32_t adler0, adler1;
     adler1 = (adler >> 16) & 0xffff;
     adler0 = adler & 0xffff;
index 5c5a6c5f3cb9956380ec493ee762562cd3179871..d3f8f089e1762e1c5853822628c64f0e93cfd93c 100644 (file)
@@ -25,10 +25,6 @@ Z_FORCEINLINE static uint32_t adler32_impl(uint32_t adler, const uint8_t *buf, s
     if (UNLIKELY(len == 1))
         return adler32_copy_len_1(adler, NULL, buf, sum2, 0);
 
-    /* initial Adler-32 value (deferred check for len == 1 speed) */
-    if (UNLIKELY(buf == NULL))
-        return 1L;
-
     /* in case short lengths are provided, keep it somewhat fast */
     if (UNLIKELY(len < 16))
         return adler32_copy_len_16(adler, NULL, buf, len, sum2, 0);
diff --git a/crc32.c b/crc32.c
index 54f6ecd4208ca316808e029b454fd4dd96025f07..92a2785e56ed3febd22a6871eb01b34aa4535634 100644 (file)
--- a/crc32.c
+++ b/crc32.c
@@ -19,24 +19,28 @@ const uint32_t * Z_EXPORT PREFIX(get_crc_table)(void) {
 
 #ifdef ZLIB_COMPAT
 unsigned long Z_EXPORT PREFIX(crc32_z)(unsigned long crc, const unsigned char *buf, size_t len) {
-    if (buf == NULL) return 0;
-
+    if (buf == NULL)
+        return CRC32_INITIAL_VALUE;
     return (unsigned long)FUNCTABLE_CALL(crc32)((uint32_t)crc, buf, len);
 }
 #else
 uint32_t Z_EXPORT PREFIX(crc32_z)(uint32_t crc, const unsigned char *buf, size_t len) {
-    if (buf == NULL) return 0;
-
+    if (buf == NULL)
+        return CRC32_INITIAL_VALUE;
     return FUNCTABLE_CALL(crc32)(crc, buf, len);
 }
 #endif
 
 #ifdef ZLIB_COMPAT
 unsigned long Z_EXPORT PREFIX(crc32)(unsigned long crc, const unsigned char *buf, unsigned int len) {
+    if (buf == NULL)
+        return CRC32_INITIAL_VALUE;
     return (unsigned long)PREFIX(crc32_z)((uint32_t)crc, buf, len);
 }
 #else
 uint32_t Z_EXPORT PREFIX(crc32)(uint32_t crc, const unsigned char *buf, uint32_t len) {
+    if (buf == NULL)
+        return CRC32_INITIAL_VALUE;
     return PREFIX(crc32_z)(crc, buf, len);
 }
 #endif
index 39079395a71dbe50d57d260c65378a739855b7a2..447d1a6449c421fcda3b22d87f98dc3a9cd87077 100644 (file)
@@ -202,14 +202,6 @@ typedef struct {
 } hash_test;
 
 static const hash_test hash_tests[] = {
-  {0, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0},
-  {0, (const uint8_t *)0x0, 0xffffffff, 0x1, 0xffffffff, 0x0},
-  {255, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0}, /*  BZ 174799.  */
-  {256, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0},
-  {257, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0},
-  {32767, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0},
-  {32768, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0},
-  {32769, (const uint8_t *)0x0, 0x0, 0x1, 0x0, 0x0},
   {0, (const uint8_t *)"", 0x1, 0x1, 0x0, 0x0},
   {1, (const uint8_t *)"", 0x1, 0x10001, 0x0, 0xd202ef8d},
   {0, (const uint8_t *)"", 0xffffffff, 0x000e000e, 0xffffffff, 0xffffffff},
@@ -285,7 +277,6 @@ static const hash_test hash_tests[] = {
   {100, (const uint8_t *)"r*Fd}ef+5RJQ;+W=4jTR9)R*p!B;]Ed7tkrLi;88U7g@3v!5pk2X6D)vt,.@N8c]@yyEcKi[vwUu@.Ppm@C6%Mv*3Nw}Y,58_aH)", 0x1, 0xfbdb1e96, 0x0, 0xeaa52777},
   {100, (const uint8_t *)"h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&", 0x1, 0x47a61ec8, 0x0, 0xcd472048},
   {5552, (const uint8_t *)long_string, 0x1, 0x8b81718f, 0x0, 0x426fa73b},
-  {0, (const uint8_t *)0x0, 0x7a30360d, 0x1, 0x0, 0x0},
   {6, (const uint8_t *)"abacus", 0x1, 0x8400270, 0x7a30360d, 0xf8655a84},
   {1, (const uint8_t *)"", 0x6fd767ee, 0xd7c567ee, 0x6fd767ee, 0x95dff795},
   {1, (const uint8_t *)"a", 0xefeb7589, 0x65e475ea, 0x0, 0xe8b7be43},
index 61ec8986acb888438ecb035ccfa0f438502cba2c..725d86aa95433dd10c927b2ba91fa850ecebe6f0 100644 (file)
@@ -21,10 +21,6 @@ public:
     void adler32_copy_test(adler32_copy_func copyfunc, hash_test params) {
         ASSERT_LE(params.len, HASH_TEST_MAX_LENGTH);
 
-        if (params.buf == NULL) {
-            GTEST_SKIP();
-        }
-
         uint32_t adler = copyfunc(params.initial_adler, dstbuf, params.buf, params.len);
 
         EXPECT_EQ(adler, params.expect_adler);
index 945a2176b2e8122cf9001ac369f89a9d7949ae52..73752fe762402c982c10b92bb954a212b9279116 100644 (file)
@@ -18,14 +18,7 @@ extern "C" {
 class crc32_variant : public ::testing::TestWithParam<hash_test> {
 public:
     void hash(hash_test param, crc32_func crc32) {
-        uint32_t crc = 0;
-        if (param.buf != NULL) {
-            if (param.len) {
-                crc = crc32(param.initial_crc, param.buf, param.len);
-            } else {
-                crc = param.initial_crc;
-            }
-        }
+        uint32_t crc = crc32(param.initial_crc, param.buf, param.len);
         EXPECT_EQ(crc, param.expect_crc);
     }
 };
@@ -37,11 +30,7 @@ class crc32_align : public ::testing::TestWithParam<int> {
 public:
     void hash(int param, crc32_func crc32) {
         uint8_t *buf = (uint8_t*)zng_alloc(sizeof(uint8_t) * (128 + param));
-        if (buf != NULL) {
-            (void)crc32(0, buf + param, 128);
-        } else {
-            FAIL();
-        }
+        (void)crc32(0, buf + param, 128);
         zng_free(buf);
     }
 };
index 2fc9792b7c09330ab1764f27845205d626cda2cc..b72884325a0e4799b11a6ee2d7a328e1338b0992 100644 (file)
@@ -21,10 +21,6 @@ public:
     void crc32_copy_test(crc32_copy_func copyfunc, hash_test params) {
         ASSERT_LE(params.len, HASH_TEST_MAX_LENGTH);
 
-        if (params.buf == NULL) {
-            GTEST_SKIP();
-        }
-
         uint32_t crc = copyfunc(params.initial_crc, dstbuf, params.buf, params.len);
 
         EXPECT_EQ(crc, params.expect_crc);