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/QPDFObjectHandle.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/QPDFObjectHandle.cc')
-rw-r--r-- | libqpdf/QPDFObjectHandle.cc | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index d182f9bf..9ccfa37a 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -22,6 +22,7 @@ #include <qpdf/QTC.hh> #include <qpdf/QUtil.hh> +#include <qpdf/QIntC.hh> #include <stdexcept> #include <stdlib.h> @@ -643,7 +644,7 @@ QPDFObjectHandle::isRectangle() { return false; } - for (size_t i = 0; i < 4; ++i) + for (int i = 0; i < 4; ++i) { if (! getArrayItem(i).isNumber()) { @@ -664,7 +665,7 @@ QPDFObjectHandle::isMatrix() { return false; } - for (size_t i = 0; i < 6; ++i) + for (int i = 0; i < 6; ++i) { if (! getArrayItem(i).isNumber()) { @@ -1013,7 +1014,7 @@ QPDFObjectHandle::getUniqueResourceName(std::string const& prefix, int& min_suffix) { std::set<std::string> names = getResourceNames(); - int max_suffix = min_suffix + names.size(); + int max_suffix = min_suffix + QIntC::to_int(names.size()); while (min_suffix <= max_suffix) { std::string candidate = prefix + QUtil::int_to_string(min_suffix); @@ -1374,7 +1375,7 @@ QPDFObjectHandle::rotatePage(int angle, bool relative) if (cur_obj.getKey("/Rotate").isInteger()) { found_rotate = true; - old_angle = cur_obj.getKey("/Rotate").getIntValue(); + old_angle = cur_obj.getKey("/Rotate").getIntValueAsInt(); } else if (cur_obj.getKey("/Parent").isDictionary()) { @@ -1506,7 +1507,7 @@ QPDFObjectHandle::parse(std::string const& object_str, bool empty = false; QPDFObjectHandle result = parse(input, object_description, tokenizer, empty, 0, 0); - size_t offset = input->tell(); + size_t offset = QIntC::to_size(input->tell()); while (offset < object_str.length()) { if (! isspace(object_str.at(offset))) @@ -1618,7 +1619,7 @@ QPDFObjectHandle::parseContentStream_data( QPDFTokenizer tokenizer; tokenizer.allowEOF(); bool empty = false; - while (static_cast<size_t>(input->tell()) < length) + while (QIntC::to_size(input->tell()) < length) { QPDFObjectHandle obj = parseInternal(input, "content", tokenizer, empty, 0, 0, true); @@ -1863,8 +1864,8 @@ QPDFObjectHandle::parseInternal(PointerHolder<InputSource> input, // Try to resolve indirect objects object = newIndirect( context, - olist.at(olist.size() - 2).getIntValue(), - olist.at(olist.size() - 1).getIntValue()); + olist.at(olist.size() - 2).getIntValueAsInt(), + olist.at(olist.size() - 1).getIntValueAsInt()); olist.pop_back(); olist.pop_back(); } |