From 5e32e1dbf798962ac18d5361b1ca969f7c47323f Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Thu, 12 Aug 2021 15:19:50 -0400 Subject: [PATCH] add a redundant loop condition to silence GCC warnings See: https://github.com/BLAKE3-team/BLAKE3/issues/94 https://github.com/BLAKE3-team/BLAKE3/issues/183 https://github.com/BLAKE3-team/BLAKE3/issues/189 --- c/blake3.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/c/blake3.c b/c/blake3.c index 9998f75c7..19cac5a5e 100644 --- a/c/blake3.c +++ b/c/blake3.c @@ -340,12 +340,19 @@ INLINE void compress_subtree_to_parent_node( uint8_t cv_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN]; size_t num_cvs = blake3_compress_subtree_wide(input, input_len, key, chunk_counter, flags, cv_array); + bool num_cvs_is_reasonable = (num_cvs <= MAX_SIMD_DEGREE_OR_2); + assert(num_cvs_is_reasonable); // If MAX_SIMD_DEGREE is greater than 2 and there's enough input, // compress_subtree_wide() returns more than 2 chaining values. Condense // them into 2 by forming parent nodes repeatedly. uint8_t out_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN / 2]; - while (num_cvs > 2) { + // num_cvs_is_reasonable is always true, and we just asserted it above. But + // GCC can't tell that it's always true, and if NDEBUG is set on platforms + // where MAX_SIMD_DEGREE_OR_2 == 2, GCC emits spurious warnings here. + // Including num_cvs_is_reasonable in this loop condition suppresses those + // warnings. + while (num_cvs > 2 && num_cvs_is_reasonable) { num_cvs = compress_parents_parallel(cv_array, num_cvs, key, flags, out_array); memcpy(cv_array, out_array, num_cvs * BLAKE3_OUT_LEN);