aboutsummaryrefslogtreecommitdiffstats
path: root/include
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 /include
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 'include')
-rw-r--r--include/qpdf/BufferInputSource.hh2
-rw-r--r--include/qpdf/Pl_Count.hh2
-rw-r--r--include/qpdf/QPDF.hh19
-rw-r--r--include/qpdf/QPDFWriter.hh12
4 files changed, 26 insertions, 9 deletions
diff --git a/include/qpdf/BufferInputSource.hh b/include/qpdf/BufferInputSource.hh
index 3fb0625b..51bb98f9 100644
--- a/include/qpdf/BufferInputSource.hh
+++ b/include/qpdf/BufferInputSource.hh
@@ -54,6 +54,8 @@ class BufferInputSource: public InputSource
virtual void unreadCh(char ch);
private:
+ qpdf_offset_t const bufSizeAsOffset() const;
+
bool own_memory;
std::string description;
Buffer* buf;
diff --git a/include/qpdf/Pl_Count.hh b/include/qpdf/Pl_Count.hh
index c3f7b3e1..0b021d9a 100644
--- a/include/qpdf/Pl_Count.hh
+++ b/include/qpdf/Pl_Count.hh
@@ -48,6 +48,8 @@ class Pl_Count: public Pipeline
unsigned char getLastChar() const;
private:
+ // Must be qpdf_offset_t, not size_t, to handle writing more than
+ // size_t can handle.
qpdf_offset_t count;
unsigned char last_char;
};
diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh
index 6de51ef3..c3fa0873 100644
--- a/include/qpdf/QPDF.hh
+++ b/include/qpdf/QPDF.hh
@@ -32,6 +32,7 @@
#include <iostream>
#include <vector>
+#include <qpdf/QIntC.hh>
#include <qpdf/QPDFExc.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObjGen.hh>
@@ -859,7 +860,7 @@ class QPDF
bool pipeForeignStreamData(
PointerHolder<ForeignStreamData>,
Pipeline*,
- unsigned long encode_flags,
+ int encode_flags,
qpdf_stream_decode_level_e decode_level);
static bool pipeStreamData(PointerHolder<QPDF::EncryptionParameters> encp,
PointerHolder<InputSource> file,
@@ -1253,7 +1254,7 @@ class QPDF
void dumpHPageOffset();
void dumpHSharedObject();
void dumpHGeneric(HGeneric&);
- int adjusted_offset(int offset);
+ qpdf_offset_t adjusted_offset(qpdf_offset_t offset);
QPDFObjectHandle objGenToIndirect(QPDFObjGen const&);
void calculateLinearizationData(
std::map<int, int> const& object_stream_data);
@@ -1297,6 +1298,20 @@ class QPDF
std::set<QPDFObjGen>& visited, bool top);
void filterCompressedObjects(std::map<int, int> const& object_stream_data);
+ // Type conversion helper methods
+ template<typename T> static qpdf_offset_t toO(T const& i)
+ {
+ return QIntC::to_offset(i);
+ }
+ template<typename T> static size_t toS(T const& i)
+ {
+ return QIntC::to_size(i);
+ }
+ template<typename T> static int toI(T const& i)
+ {
+ return QIntC::to_int(i);
+ }
+
class Members
{
friend class QPDF;
diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh
index 0b8bf028..2ac00582 100644
--- a/include/qpdf/QPDFWriter.hh
+++ b/include/qpdf/QPDFWriter.hh
@@ -467,7 +467,7 @@ class QPDFWriter
enum trailer_e { t_normal, t_lin_first, t_lin_second };
- int bytesNeeded(unsigned long long n);
+ unsigned int bytesNeeded(long long n);
void writeBinary(unsigned long long val, unsigned int bytes);
void writeString(std::string const& str);
void writeBuffer(PointerHolder<Buffer>&);
@@ -483,10 +483,8 @@ class QPDFWriter
void writeTrailer(trailer_e which, int size,
bool xref_stream, qpdf_offset_t prev,
int linearization_pass);
- void unparseObject(QPDFObjectHandle object, int level,
- unsigned int flags);
- void unparseObject(QPDFObjectHandle object, int level,
- unsigned int flags,
+ void unparseObject(QPDFObjectHandle object, int level, int flags);
+ void unparseObject(QPDFObjectHandle object, int level, int flags,
// for stream dictionaries
size_t stream_length, bool compress);
void unparseChild(QPDFObjectHandle child, int level, int flags);
@@ -510,7 +508,7 @@ class QPDFWriter
char const* user_password, char const* owner_password,
int V, int R, int key_len, std::set<int>& bits_to_clear);
void setEncryptionParametersInternal(
- int V, int R, int key_len, long P,
+ int V, int R, int key_len, int P,
std::string const& O, std::string const& U,
std::string const& OE, std::string const& UE, std::string const& Perms,
std::string const& id1, std::string const& user_password,
@@ -554,7 +552,7 @@ class QPDFWriter
qpdf_offset_t hint_length,
bool skip_compression,
int linearization_pass);
- int calculateXrefStreamPadding(int xref_bytes);
+ int calculateXrefStreamPadding(qpdf_offset_t xref_bytes);
// When filtering subsections, push additional pipelines to the
// stack. When ready to switch, activate the pipeline stack.