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/Pl_LZWDecoder.cc | |
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/Pl_LZWDecoder.cc')
-rw-r--r-- | libqpdf/Pl_LZWDecoder.cc | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/libqpdf/Pl_LZWDecoder.cc b/libqpdf/Pl_LZWDecoder.cc index 2cc2077b..6cc87048 100644 --- a/libqpdf/Pl_LZWDecoder.cc +++ b/libqpdf/Pl_LZWDecoder.cc @@ -2,6 +2,7 @@ #include <qpdf/QTC.hh> #include <qpdf/QUtil.hh> +#include <qpdf/QIntC.hh> #include <stdexcept> #include <string.h> #include <assert.h> @@ -52,22 +53,22 @@ Pl_LZWDecoder::finish() void Pl_LZWDecoder::sendNextCode() { - int high = this->byte_pos; - int med = (this->byte_pos + 1) % 3; - int low = (this->byte_pos + 2) % 3; + unsigned int high = this->byte_pos; + unsigned int med = (this->byte_pos + 1) % 3; + unsigned int low = (this->byte_pos + 2) % 3; - int bits_from_high = 8 - this->bit_pos; - int bits_from_med = this->code_size - bits_from_high; - int bits_from_low = 0; + unsigned int bits_from_high = 8 - this->bit_pos; + unsigned int bits_from_med = this->code_size - bits_from_high; + unsigned int bits_from_low = 0; if (bits_from_med > 8) { bits_from_low = bits_from_med - 8; bits_from_med = 8; } - int high_mask = (1 << bits_from_high) - 1; - int med_mask = 0xff - ((1 << (8 - bits_from_med)) - 1); - int low_mask = 0xff - ((1 << (8 - bits_from_low)) - 1); - int code = 0; + unsigned int high_mask = (1U << bits_from_high) - 1U; + unsigned int med_mask = 0xff - ((1U << (8 - bits_from_med)) - 1U); + unsigned int low_mask = 0xff - ((1U << (8 - bits_from_low)) - 1U); + unsigned int code = 0; code += (this->buf[high] & high_mask) << bits_from_med; code += ((this->buf[med] & med_mask) >> (8 - bits_from_med)); if (bits_from_low) @@ -94,7 +95,7 @@ Pl_LZWDecoder::sendNextCode() } unsigned char -Pl_LZWDecoder::getFirstChar(int code) +Pl_LZWDecoder::getFirstChar(unsigned int code) { unsigned char result = '\0'; if (code < 256) @@ -130,7 +131,7 @@ Pl_LZWDecoder::addToTable(unsigned char next) if (this->last_code < 256) { - tmp[0] = this->last_code; + tmp[0] = static_cast<unsigned char>(this->last_code); last_data = tmp; last_size = 1; } @@ -144,7 +145,7 @@ Pl_LZWDecoder::addToTable(unsigned char next) } Buffer& b = table.at(idx); last_data = b.getBuffer(); - last_size = b.getSize(); + last_size = QIntC::to_uint(b.getSize()); } else { @@ -161,7 +162,7 @@ Pl_LZWDecoder::addToTable(unsigned char next) } void -Pl_LZWDecoder::handleCode(int code) +Pl_LZWDecoder::handleCode(unsigned int code) { if (this->eod) { @@ -189,11 +190,11 @@ Pl_LZWDecoder::handleCode(int code) // be what we read last plus the first character of what // we're reading now. unsigned char next = '\0'; - unsigned int table_size = table.size(); + unsigned int table_size = QIntC::to_uint(table.size()); if (code < 256) { // just read < 256; last time's next was code - next = code; + next = static_cast<unsigned char>(code); } else if (code > 257) { |