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_DCT.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_DCT.cc')
-rw-r--r-- | libqpdf/Pl_DCT.cc | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/libqpdf/Pl_DCT.cc b/libqpdf/Pl_DCT.cc index ceecc518..4da068cb 100644 --- a/libqpdf/Pl_DCT.cc +++ b/libqpdf/Pl_DCT.cc @@ -2,6 +2,7 @@ #include <qpdf/QUtil.hh> #include <qpdf/QTC.hh> +#include <qpdf/QIntC.hh> #include <setjmp.h> #include <stdexcept> @@ -213,7 +214,7 @@ skip_buffer_input_data(j_decompress_ptr cinfo, long num_bytes) "reading jpeg: jpeg library requested" " skipping a negative number of bytes"); } - size_t to_skip = static_cast<size_t>(num_bytes); + size_t to_skip = QIntC::to_size(num_bytes); if ((to_skip > 0) && (to_skip <= cinfo->src->bytes_in_buffer)) { cinfo->src->next_input_byte += to_skip; @@ -283,15 +284,17 @@ Pl_DCT::compress(void* cinfo_p, Buffer* b) jpeg_start_compress(cinfo, TRUE); - int width = cinfo->image_width * cinfo->input_components; + unsigned int width = cinfo->image_width * + QIntC::to_uint(cinfo->input_components); size_t expected_size = - cinfo->image_height * cinfo->image_width * cinfo->input_components; + cinfo->image_height * cinfo->image_width * + QIntC::to_uint(cinfo->input_components); if (b->getSize() != expected_size) { throw std::runtime_error( "Pl_DCT: image buffer size = " + - QUtil::int_to_string(b->getSize()) + "; expected size = " + - QUtil::int_to_string(expected_size)); + QUtil::uint_to_string(b->getSize()) + "; expected size = " + + QUtil::uint_to_string(expected_size)); } JSAMPROW row_pointer[1]; unsigned char* buffer = b->getBuffer(); @@ -326,7 +329,8 @@ Pl_DCT::decompress(void* cinfo_p, Buffer* b) (void) jpeg_read_header(cinfo, TRUE); (void) jpeg_calc_output_dimensions(cinfo); - int width = cinfo->output_width * cinfo->output_components; + unsigned int width = cinfo->output_width * + QIntC::to_uint(cinfo->output_components); JSAMPARRAY buffer = (*cinfo->mem->alloc_sarray) (reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE, width, 1); |