From d71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 20 Jun 2019 23:35:23 -0400 Subject: Fix sign and conversion warnings (major) This makes all integer type conversions that have potential data loss explicit with calls that do range checks and raise an exception. After this commit, qpdf builds with no warnings when -Wsign-conversion -Wconversion is used with gcc or clang or when -W3 -Wd4800 is used with MSVC. This significantly reduces the likelihood of potential crashes from bogus integer values. There are some parts of the code that take int when they should take size_t or an offset. Such places would make qpdf not support files with more than 2^31 of something that usually wouldn't be so large. In the event that such a file shows up and is valid, at least qpdf would raise an error in the right spot so the issue could be legitimately addressed rather than failing in some weird way because of a silent overflow condition. --- libqpdf/bits.icc | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'libqpdf/bits.icc') diff --git a/libqpdf/bits.icc b/libqpdf/bits.icc index bcd7dd85..1cbbebcc 100644 --- a/libqpdf/bits.icc +++ b/libqpdf/bits.icc @@ -16,8 +16,8 @@ #ifdef BITS_READ static unsigned long long -read_bits(unsigned char const*& p, unsigned int& bit_offset, - unsigned int& bits_available, unsigned int bits_wanted) +read_bits(unsigned char const*& p, size_t& bit_offset, + size_t& bits_available, size_t bits_wanted) { // View p as a stream of bits: @@ -46,11 +46,12 @@ read_bits(unsigned char const*& p, unsigned int& bit_offset, { // Grab bits from the first byte clearing anything before // bit_offset. - unsigned char byte = *p & ((1 << (bit_offset + 1)) - 1); + unsigned char byte = static_cast( + *p & ((1U << (bit_offset + 1U)) - 1U)); // There are bit_offset + 1 bits available in the first byte. - unsigned int to_copy = std::min(bits_wanted, bit_offset + 1); - unsigned int leftover = (bit_offset + 1) - to_copy; + size_t to_copy = std::min(bits_wanted, bit_offset + 1); + size_t leftover = (bit_offset + 1) - to_copy; #ifdef BITS_TESTING QTC::TC("libtests", "bits bit_offset", @@ -61,7 +62,7 @@ read_bits(unsigned char const*& p, unsigned int& bit_offset, #endif // Right shift so that all the bits we want are right justified. - byte >>= leftover; + byte = static_cast(byte >> leftover); // Copy the bits into result result <<= to_copy; @@ -94,8 +95,8 @@ read_bits(unsigned char const*& p, unsigned int& bit_offset, #ifdef BITS_WRITE static void -write_bits(unsigned char& ch, unsigned int& bit_offset, - unsigned long long val, unsigned int bits, Pipeline* pipeline) +write_bits(unsigned char& ch, size_t& bit_offset, + unsigned long long val, size_t bits, Pipeline* pipeline) { if (bits > 32) { @@ -111,11 +112,11 @@ write_bits(unsigned char& ch, unsigned int& bit_offset, #endif while (bits > 0) { - int bits_to_write = std::min(bits, bit_offset + 1); - unsigned char newval = - (val >> (bits - bits_to_write)) & ((1 << bits_to_write) - 1); - int bits_left_in_ch = bit_offset + 1 - bits_to_write; - newval <<= bits_left_in_ch; + size_t bits_to_write = std::min(bits, bit_offset + 1); + unsigned char newval = static_cast( + (val >> (bits - bits_to_write)) & ((1U << bits_to_write) - 1)); + size_t bits_left_in_ch = bit_offset + 1 - bits_to_write; + newval = static_cast(newval << bits_left_in_ch); ch |= newval; if (bits_left_in_ch == 0) { -- cgit v1.2.3-54-g00ecf