Commit f40ffc9d6392edf9b6fe74d288d6d578e6d1a240

Authored by Jay Berkenbilt
1 parent da30764b

Pl_Flate: constructor's out_bufsize is now unsigned int

This is the type we need for the underlying zlib implementation.
ChangeLog
1 1 2019-06-20 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * Change out_bufsize argument to Pl_Flate's constructor for int to
  4 + unsigned int for compatibility with underlying zlib
  5 + implementation.
  6 +
3 7 * Add methods to QPDFObjectHandle to return the value of Integer
4 8 objects as int and unsigned int with range checking and fallback
5 9 behavior to avoid silent underflow/overflow conditions.
... ...
include/qpdf/Pl_Flate.hh
... ... @@ -27,13 +27,13 @@
27 27 class Pl_Flate: public Pipeline
28 28 {
29 29 public:
30   - static int const def_bufsize = 65536;
  30 + static unsigned int const def_bufsize = 65536;
31 31  
32 32 enum action_e { a_inflate, a_deflate };
33 33  
34 34 QPDF_DLL
35 35 Pl_Flate(char const* identifier, Pipeline* next,
36   - action_e action, int out_bufsize = def_bufsize);
  36 + action_e action, unsigned int out_bufsize = def_bufsize);
37 37 QPDF_DLL
38 38 virtual ~Pl_Flate();
39 39  
... ... @@ -43,11 +43,11 @@ class Pl_Flate: public Pipeline
43 43 virtual void finish();
44 44  
45 45 private:
46   - void handleData(unsigned char* data, int len, int flush);
  46 + void handleData(unsigned char* data, size_t len, int flush);
47 47 void checkError(char const* prefix, int error_code);
48 48  
49 49 unsigned char* outbuf;
50   - int out_bufsize;
  50 + size_t out_bufsize;
51 51 action_e action;
52 52 bool initialized;
53 53 void* zdata;
... ...
libqpdf/Pl_Flate.cc
1 1 #include <qpdf/Pl_Flate.hh>
2 2 #include <zlib.h>
3 3 #include <string.h>
  4 +#include <limits.h>
4 5  
5 6 #include <qpdf/QUtil.hh>
  7 +#include <qpdf/QIntC.hh>
6 8  
7 9 Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
8   - action_e action, int out_bufsize) :
  10 + action_e action, unsigned int out_bufsize_int) :
9 11 Pipeline(identifier, next),
10   - out_bufsize(out_bufsize),
  12 + out_bufsize(QIntC::to_size(out_bufsize_int)),
11 13 action(action),
12 14 initialized(false)
13 15 {
... ... @@ -19,6 +21,13 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
19 21 // Windows environment.
20 22 this->zdata = new z_stream;
21 23  
  24 + if (out_bufsize > UINT_MAX)
  25 + {
  26 + throw std::runtime_error(
  27 + "Pl_Flate: zlib doesn't support buffer"
  28 + " sizes larger than unsigned int");
  29 + }
  30 +
22 31 z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
23 32 zstream.zalloc = 0;
24 33 zstream.zfree = 0;
... ... @@ -26,7 +35,7 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
26 35 zstream.next_in = 0;
27 36 zstream.avail_in = 0;
28 37 zstream.next_out = this->outbuf;
29   - zstream.avail_out = out_bufsize;
  38 + zstream.avail_out = QIntC::to_uint(out_bufsize);
30 39 }
31 40  
32 41 Pl_Flate::~Pl_Flate()
... ... @@ -77,11 +86,17 @@ Pl_Flate::write(unsigned char* data, size_t len)
77 86 }
78 87  
79 88 void
80   -Pl_Flate::handleData(unsigned char* data, int len, int flush)
  89 +Pl_Flate::handleData(unsigned char* data, size_t len, int flush)
81 90 {
  91 + if (len > UINT_MAX)
  92 + {
  93 + throw std::runtime_error(
  94 + "Pl_Flate: zlib doesn't support data"
  95 + " blocks larger than int");
  96 + }
82 97 z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
83 98 zstream.next_in = data;
84   - zstream.avail_in = len;
  99 + zstream.avail_in = QIntC::to_uint(len);
85 100  
86 101 if (! this->initialized)
87 102 {
... ... @@ -156,12 +171,13 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
156 171 // needed, so we're done for now.
157 172 done = true;
158 173 }
159   - uLong ready = (this->out_bufsize - zstream.avail_out);
  174 + uLong ready =
  175 + QIntC::to_ulong(this->out_bufsize - zstream.avail_out);
160 176 if (ready > 0)
161 177 {
162 178 this->getNext()->write(this->outbuf, ready);
163 179 zstream.next_out = this->outbuf;
164   - zstream.avail_out = this->out_bufsize;
  180 + zstream.avail_out = QIntC::to_uint(this->out_bufsize);
165 181 }
166 182 }
167 183 break;
... ...