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/BufferInputSource.cc | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'libqpdf/BufferInputSource.cc') diff --git a/libqpdf/BufferInputSource.cc b/libqpdf/BufferInputSource.cc index 4346fcd0..1bf61bbd 100644 --- a/libqpdf/BufferInputSource.cc +++ b/libqpdf/BufferInputSource.cc @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -32,6 +33,12 @@ BufferInputSource::~BufferInputSource() } } +qpdf_offset_t const +BufferInputSource::bufSizeAsOffset() const +{ + return QIntC::to_offset(this->buf->getSize()); +} + qpdf_offset_t BufferInputSource::findAndSkipNextEOL() { @@ -39,7 +46,7 @@ BufferInputSource::findAndSkipNextEOL() { throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0"); } - qpdf_offset_t end_pos = this->buf->getSize(); + qpdf_offset_t end_pos = bufSizeAsOffset(); if (this->cur_offset >= end_pos) { this->last_offset = end_pos; @@ -48,7 +55,7 @@ BufferInputSource::findAndSkipNextEOL() } qpdf_offset_t result = 0; - size_t len = end_pos - this->cur_offset; + size_t len = QIntC::to_size(end_pos - this->cur_offset); unsigned char const* buffer = this->buf->getBuffer(); void* start = const_cast(buffer) + this->cur_offset; @@ -97,7 +104,7 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence) break; case SEEK_END: - this->cur_offset = this->buf->getSize() + offset; + this->cur_offset = bufSizeAsOffset() + offset; break; case SEEK_CUR: @@ -130,7 +137,7 @@ BufferInputSource::read(char* buffer, size_t length) { throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0"); } - qpdf_offset_t end_pos = this->buf->getSize(); + qpdf_offset_t end_pos = bufSizeAsOffset(); if (this->cur_offset >= end_pos) { this->last_offset = end_pos; @@ -139,9 +146,9 @@ BufferInputSource::read(char* buffer, size_t length) this->last_offset = this->cur_offset; size_t len = std::min( - static_cast(end_pos - this->cur_offset), length); + QIntC::to_size(end_pos - this->cur_offset), length); memcpy(buffer, buf->getBuffer() + this->cur_offset, len); - this->cur_offset += len; + this->cur_offset += QIntC::to_offset(len); return len; } -- cgit v1.2.3-70-g09d2