aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/BitStream.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/BitStream.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/BitStream.cc')
-rw-r--r--libqpdf/BitStream.cc24
1 files changed, 17 insertions, 7 deletions
diff --git a/libqpdf/BitStream.cc b/libqpdf/BitStream.cc
index bb52af31..95ca47c5 100644
--- a/libqpdf/BitStream.cc
+++ b/libqpdf/BitStream.cc
@@ -1,10 +1,11 @@
#include <qpdf/BitStream.hh>
+#include <qpdf/QIntC.hh>
// See comments in bits.cc
#define BITS_READ 1
#include "bits.icc"
-BitStream::BitStream(unsigned char const* p, int nbytes) :
+BitStream::BitStream(unsigned char const* p, size_t nbytes) :
start(p),
nbytes(nbytes)
{
@@ -16,7 +17,7 @@ BitStream::reset()
{
p = start;
bit_offset = 7;
- if (static_cast<unsigned int>(nbytes) > static_cast<unsigned int>(-1) / 8)
+ if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8)
{
throw std::runtime_error("array too large for bitstream");
}
@@ -24,21 +25,21 @@ BitStream::reset()
}
unsigned long long
-BitStream::getBits(int nbits)
+BitStream::getBits(size_t nbits)
{
return read_bits(this->p, this->bit_offset,
this->bits_available, nbits);
}
long long
-BitStream::getBitsSigned(int nbits)
+BitStream::getBitsSigned(size_t nbits)
{
unsigned long long bits = read_bits(this->p, this->bit_offset,
this->bits_available, nbits);
long long result = 0;
- if (static_cast<long long>(bits) > 1 << (nbits - 1))
+ if (static_cast<long long>(bits) > 1LL << (nbits - 1))
{
- result = static_cast<long long>(bits - (1 << nbits));
+ result = static_cast<long long>(bits -(1ULL << nbits));
}
else
{
@@ -47,12 +48,21 @@ BitStream::getBitsSigned(int nbits)
return result;
}
+int
+BitStream::getBitsInt(size_t nbits)
+{
+ return static_cast<int>(
+ QIntC::to_uint(
+ read_bits(this->p, this->bit_offset,
+ this->bits_available, nbits)));
+}
+
void
BitStream::skipToNextByte()
{
if (bit_offset != 7)
{
- unsigned int bits_to_skip = bit_offset + 1;
+ size_t bits_to_skip = bit_offset + 1;
if (bits_available < bits_to_skip)
{
throw std::logic_error(