aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/BitWriter.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/BitWriter.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/BitWriter.cc')
-rw-r--r--libqpdf/BitWriter.cc14
1 files changed, 10 insertions, 4 deletions
diff --git a/libqpdf/BitWriter.cc b/libqpdf/BitWriter.cc
index 7513ac76..efe19ded 100644
--- a/libqpdf/BitWriter.cc
+++ b/libqpdf/BitWriter.cc
@@ -12,18 +12,18 @@ BitWriter::BitWriter(Pipeline* pl) :
}
void
-BitWriter::writeBits(unsigned long long val, unsigned int bits)
+BitWriter::writeBits(unsigned long long val, size_t bits)
{
write_bits(this->ch, this->bit_offset, val, bits, this->pl);
}
void
-BitWriter::writeBitsSigned(long long val, unsigned int bits)
+BitWriter::writeBitsSigned(long long val, size_t bits)
{
unsigned long long uval = 0;
if (val < 0)
{
- uval = static_cast<unsigned long long>((1 << bits) + val);
+ uval = (1ULL << bits) + static_cast<unsigned long long>(val);
}
else
{
@@ -33,11 +33,17 @@ BitWriter::writeBitsSigned(long long val, unsigned int bits)
}
void
+BitWriter::writeBitsInt(int val, size_t bits)
+{
+ writeBits(static_cast<unsigned long long>(val), bits);
+}
+
+void
BitWriter::flush()
{
if (bit_offset < 7)
{
- int bits_to_write = bit_offset + 1;
+ size_t bits_to_write = bit_offset + 1;
write_bits(this->ch, this->bit_offset, 0, bits_to_write, this->pl);
}
}