aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/bits.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 /libtests/bits.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 'libtests/bits.cc')
-rw-r--r--libtests/bits.cc32
1 files changed, 17 insertions, 15 deletions
diff --git a/libtests/bits.cc b/libtests/bits.cc
index 3b274f93..1dfd42ed 100644
--- a/libtests/bits.cc
+++ b/libtests/bits.cc
@@ -1,6 +1,8 @@
#include <qpdf/BitStream.hh>
#include <qpdf/BitWriter.hh>
#include <qpdf/Pl_Buffer.hh>
+#include <qpdf/QUtil.hh>
+#include <qpdf/QIntC.hh>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
@@ -12,8 +14,8 @@
#include "../libqpdf/bits.icc"
static void
-print_values(int byte_offset, unsigned int bit_offset,
- unsigned int bits_available)
+print_values(long long byte_offset, size_t bit_offset,
+ size_t bits_available)
{
std::cout << "byte offset = " << byte_offset << ", "
<< "bit offset = " << bit_offset << ", "
@@ -22,11 +24,11 @@ print_values(int byte_offset, unsigned int bit_offset,
static void
test_read_bits(unsigned char const* buf,
- unsigned char const*& p, unsigned int& bit_offset,
- unsigned int& bits_available, int bits_wanted)
+ unsigned char const*& p, size_t& bit_offset,
+ size_t& bits_available, size_t bits_wanted)
{
unsigned long result =
- read_bits(p, bit_offset, bits_available, bits_wanted);
+ QIntC::to_ulong(read_bits(p, bit_offset, bits_available, bits_wanted));
std::cout << "bits read: " << bits_wanted << ", result = " << result
<< std::endl;
@@ -34,12 +36,12 @@ test_read_bits(unsigned char const* buf,
}
static void
-test_write_bits(unsigned char& ch, unsigned int& bit_offset, unsigned long val,
- int bits, Pl_Buffer* bp)
+test_write_bits(unsigned char& ch, size_t& bit_offset, unsigned long val,
+ size_t bits, Pl_Buffer* bp)
{
write_bits(ch, bit_offset, val, bits, bp);
- printf("ch = %02x, bit_offset = %d\n",
- static_cast<unsigned int>(ch), bit_offset);
+ std::cout << "ch = " << QUtil::uint_to_string_base(ch, 16, 2)
+ << ", bit_offset = " << bit_offset << std::endl;
}
static void
@@ -51,10 +53,10 @@ print_buffer(Pl_Buffer* bp)
size_t l = b->getSize();
for (unsigned long i = 0; i < l; ++i)
{
- printf("%02x%s", static_cast<unsigned int>(p[i]),
- (i == l - 1) ? "\n" : " ");
+ std::cout << QUtil::uint_to_string_base(p[i], 16, 2)
+ << ((i == l - 1) ? "\n" : " ");
}
- printf("\n");
+ std::cout << std::endl;
delete b;
}
@@ -71,8 +73,8 @@ test()
};
unsigned char const* p = buf;
- unsigned int bit_offset = 7;
- unsigned int bits_available = 64;
+ size_t bit_offset = 7;
+ size_t bits_available = 64;
// 11110:101 0:001010:1 01100101: 01111001
// 0:00:1:0010 10001001 01110101 01001:011
@@ -163,7 +165,7 @@ test()
bw.writeBits(30UL, 5);
bw.flush();
bw.flush();
- bw.writeBits(0xABUL, 8);
+ bw.writeBitsInt(0xAB, 8);
bw.flush();
print_buffer(bp);
bw.writeBitsSigned(-1, 3); // 111