aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/BufferInputSource.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-06-21 05:35:23 +0200
committerJay Berkenbilt <ejb@ql.org>2019-06-21 19:17:21 +0200
commitd71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e (patch)
tree60f4a13cbadee1a02a49f35f325460f2ad507c95 /libqpdf/BufferInputSource.cc
parentf40ffc9d6392edf9b6fe74d288d6d578e6d1a240 (diff)
downloadqpdf-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/BufferInputSource.cc')
-rw-r--r--libqpdf/BufferInputSource.cc19
1 files changed, 13 insertions, 6 deletions
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 <qpdf/BufferInputSource.hh>
+#include <qpdf/QIntC.hh>
#include <string.h>
#include <stdexcept>
#include <algorithm>
@@ -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<unsigned char*>(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<size_t>(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;
}