From: Nathan Moinvaziri Date: Mon, 12 Jan 2026 19:18:56 +0000 (-0800) Subject: Clean up buf == NULL handling on adler32 functions and test strings. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d684da4ff54502d91331548c8fda18aab3680ed6;p=thirdparty%2Fzlib-ng.git Clean up buf == NULL handling on adler32 functions and test strings. --- diff --git a/adler32.c b/adler32.c index 1a643ed53..3e3dfca7e 100644 --- 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 diff --git a/arch/arm/adler32_neon.c b/arch/arm/adler32_neon.c index 4943bb9ff..0b41e4d2b 100644 --- a/arch/arm/adler32_neon.c +++ b/arch/arm/adler32_neon.c @@ -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); diff --git a/arch/generic/adler32_c.c b/arch/generic/adler32_c.c index d7179da8e..4bd553816 100644 --- a/arch/generic/adler32_c.c +++ b/arch/generic/adler32_c.c @@ -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); diff --git a/arch/loongarch/adler32_lasx.c b/arch/loongarch/adler32_lasx.c index c63930551..d93f41b0e 100644 --- a/arch/loongarch/adler32_lasx.c +++ b/arch/loongarch/adler32_lasx.c @@ -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; diff --git a/arch/loongarch/adler32_lsx.c b/arch/loongarch/adler32_lsx.c index 75b3b4550..8f277b43e 100644 --- a/arch/loongarch/adler32_lsx.c +++ b/arch/loongarch/adler32_lsx.c @@ -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; diff --git a/arch/power/adler32_power8.c b/arch/power/adler32_power8.c index 10338fcc8..dfa3f7beb 100644 --- a/arch/power/adler32_power8.c +++ b/arch/power/adler32_power8.c @@ -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); diff --git a/arch/power/adler32_vmx.c b/arch/power/adler32_vmx.c index 03a847430..e8a6bd71a 100644 --- a/arch/power/adler32_vmx.c +++ b/arch/power/adler32_vmx.c @@ -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); diff --git a/arch/power/crc32_power8.c b/arch/power/crc32_power8.c index 914cca8ab..05e117023 100644 --- a/arch/power/crc32_power8.c +++ b/arch/power/crc32_power8.c @@ -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) { diff --git a/arch/riscv/adler32_rvv.c b/arch/riscv/adler32_rvv.c index b9360ed72..9ae31b301 100644 --- a/arch/riscv/adler32_rvv.c +++ b/arch/riscv/adler32_rvv.c @@ -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); diff --git a/arch/x86/adler32_avx2.c b/arch/x86/adler32_avx2.c index 464e83df0..bf748eb8f 100644 --- a/arch/x86/adler32_avx2.c +++ b/arch/x86/adler32_avx2.c @@ -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; diff --git a/arch/x86/adler32_avx512.c b/arch/x86/adler32_avx512.c index 545806bfc..5fffbd360 100644 --- a/arch/x86/adler32_avx512.c +++ b/arch/x86/adler32_avx512.c @@ -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; diff --git a/arch/x86/adler32_avx512_vnni.c b/arch/x86/adler32_avx512_vnni.c index 9fbb3bf38..741176cac 100644 --- a/arch/x86/adler32_avx512_vnni.c +++ b/arch/x86/adler32_avx512_vnni.c @@ -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; diff --git a/arch/x86/adler32_ssse3.c b/arch/x86/adler32_ssse3.c index 5c5a6c5f3..d3f8f089e 100644 --- a/arch/x86/adler32_ssse3.c +++ b/arch/x86/adler32_ssse3.c @@ -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 54f6ecd42..92a2785e5 100644 --- 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 diff --git a/test/hash_test_strings_p.h b/test/hash_test_strings_p.h index 39079395a..447d1a644 100644 --- a/test/hash_test_strings_p.h +++ b/test/hash_test_strings_p.h @@ -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}, diff --git a/test/test_adler32_copy.cc b/test/test_adler32_copy.cc index 61ec8986a..725d86aa9 100644 --- a/test/test_adler32_copy.cc +++ b/test/test_adler32_copy.cc @@ -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); diff --git a/test/test_crc32.cc b/test/test_crc32.cc index 945a2176b..73752fe76 100644 --- a/test/test_crc32.cc +++ b/test/test_crc32.cc @@ -18,14 +18,7 @@ extern "C" { class crc32_variant : public ::testing::TestWithParam { 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 { 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); } }; diff --git a/test/test_crc32_copy.cc b/test/test_crc32_copy.cc index 2fc9792b7..b72884325 100644 --- a/test/test_crc32_copy.cc +++ b/test/test_crc32_copy.cc @@ -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);