diff options
author | Jay Berkenbilt <ejb@ql.org> | 2013-10-05 22:37:27 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2013-10-10 02:57:14 +0200 |
commit | e19eb579b221ade503d7d1ff0a6511d289863785 (patch) | |
tree | 8e0d330ff9d4132eadacc399affb7bcb1a1b5933 /libqpdf/Pl_LZWDecoder.cc | |
parent | 0bfe9024893ebb1f62108fe6c24410e6ba589c3e (diff) | |
download | qpdf-e19eb579b221ade503d7d1ff0a6511d289863785.tar.zst |
Replace some assertions with std::logic_error
Ideally, the library should never call assert outside of test code,
but it does in several places. For some cases where the assertion
might conceivably fail because of a problem with the input data,
replace assertions with exceptions so that they can be trapped by the
calling application. This commit surely misses some cases and
replaced some cases unnecessarily, but it should still be an
improvement.
Diffstat (limited to 'libqpdf/Pl_LZWDecoder.cc')
-rw-r--r-- | libqpdf/Pl_LZWDecoder.cc | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/libqpdf/Pl_LZWDecoder.cc b/libqpdf/Pl_LZWDecoder.cc index f7c6fb30..82a668e3 100644 --- a/libqpdf/Pl_LZWDecoder.cc +++ b/libqpdf/Pl_LZWDecoder.cc @@ -1,6 +1,7 @@ #include <qpdf/Pl_LZWDecoder.hh> #include <qpdf/QTC.hh> +#include <qpdf/QUtil.hh> #include <stdexcept> #include <string.h> #include <assert.h> @@ -100,14 +101,23 @@ Pl_LZWDecoder::getFirstChar(int code) { result = static_cast<unsigned char>(code); } - else + else if (code > 257) { - assert(code > 257); unsigned int idx = code - 258; - assert(idx < table.size()); + if (idx >= table.size()) + { + throw std::logic_error( + "Pl_LZWDecoder::getFirstChar: table overflow"); + } Buffer& b = table[idx]; result = b.getBuffer()[0]; } + else + { + throw std::logic_error( + "Pl_LZWDecoder::getFirstChar called with invalid code (" + + QUtil::int_to_string(code) + ")"); + } return result; } @@ -124,15 +134,24 @@ Pl_LZWDecoder::addToTable(unsigned char next) last_data = tmp; last_size = 1; } - else + else if (this->last_code > 257) { - assert(this->last_code > 257); unsigned int idx = this->last_code - 258; - assert(idx < table.size()); + if (idx >= table.size()) + { + throw std::logic_error( + "Pl_LZWDecoder::addToTable: table overflow"); + } Buffer& b = table[idx]; last_data = b.getBuffer(); last_size = b.getSize(); } + else + { + throw std::logic_error( + "Pl_LZWDecoder::addToTable called with invalid code (" + + QUtil::int_to_string(this->last_code) + ")"); + } Buffer entry(1 + last_size); unsigned char* new_data = entry.getBuffer(); |