From 154bcd43bd3c615f14b38b16d0b27c4362cd0cb1 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 19 Oct 2022 12:48:40 -0400 Subject: [PATCH 1/2] Avoid >> above type width in BitWriter --- src/int_utils.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/int_utils.h b/src/int_utils.h index 37a9f3f..a7889b9 100644 --- a/src/int_utils.h +++ b/src/int_utils.h @@ -55,11 +55,10 @@ class BitWriter { int offset = 0; unsigned char* out; -public: - BitWriter(unsigned char* output) : out(output) {} - template - inline void Write(I val) { + inline void WriteInner(I val) { + // We right shift by up to 8 bits below. Verify that's well defined for the type I. + static_assert(std::numeric_limits::digits > 8, "BitWriter::WriteInner needs I > 8 bits"); int bits = BITS; if (bits + offset >= 8) { state |= ((val & ((I(1) << (8 - offset)) - 1)) << offset); @@ -78,6 +77,19 @@ class BitWriter { offset += bits; } + +public: + BitWriter(unsigned char* output) : out(output) {} + + template + inline void Write(I val) { + // If I is smaller than an unsigned int, invoke WriteInner with argument converted to unsigned. + using compute_type = typename std::conditional< + (std::numeric_limits::digits < std::numeric_limits::digits), + unsigned, I>::type; + return WriteInner(val); + } + inline void Flush() { if (offset) { *(out++) = state; From fe1040f227b30cece1f50a9b5bb439076e1afc4e Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 19 Oct 2022 12:52:32 -0400 Subject: [PATCH 2/2] Drop -Wno-shift-count-overflow compile flag --- configure.ac | 5 ----- 1 file changed, 5 deletions(-) diff --git a/configure.ac b/configure.ac index 42a28f8..cd52d7f 100644 --- a/configure.ac +++ b/configure.ac @@ -104,11 +104,6 @@ esac AX_CHECK_COMPILE_FLAG([-Wall],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wall"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],[CXXFLAGS="$CXXFLAGS -fvisibility=hidden"],[],[$CXXFLAG_WERROR]) -## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all -## unknown options if any other warning is produced. Test the -Wfoo case, and -## set the -Wno-foo case if it works. -AX_CHECK_COMPILE_FLAG([-Wshift-count-overflow],[NOWARN_CXXFLAGS="$NOWARN_CXXFLAGS -Wno-shift-count-overflow"],,[[$CXXFLAG_WERROR]]) - if test "x$use_ccache" != "xno"; then AC_MSG_CHECKING(if ccache should be used) if test x$CCACHE = x; then