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/QPDFObjectHandle.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'libqpdf/QPDFObjectHandle.cc') 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 #include +#include #include #include @@ -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 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(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 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(); } -- cgit v1.2.3-54-g00ecf