diff options
author | Jay Berkenbilt <ejb@ql.org> | 2019-06-21 05:35:23 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2019-06-21 19:17:21 +0200 |
commit | d71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e (patch) | |
tree | 60f4a13cbadee1a02a49f35f325460f2ad507c95 /libqpdf/bits.icc | |
parent | f40ffc9d6392edf9b6fe74d288d6d578e6d1a240 (diff) | |
download | qpdf-d71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e.tar.zst |
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.
Diffstat (limited to 'libqpdf/bits.icc')
-rw-r--r-- | libqpdf/bits.icc | 27 |
1 files changed, 14 insertions, 13 deletions
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<unsigned char>( + *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<unsigned char>(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<unsigned char>( + (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<unsigned char>(newval << bits_left_in_ch); ch |= newval; if (bits_left_in_ch == 0) { |