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 2019-06-20 Jay Berkenbilt <ejb@ql.org> 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 * Add methods to QPDFObjectHandle to return the value of Integer 7 * Add methods to QPDFObjectHandle to return the value of Integer
4 objects as int and unsigned int with range checking and fallback 8 objects as int and unsigned int with range checking and fallback
5 behavior to avoid silent underflow/overflow conditions. 9 behavior to avoid silent underflow/overflow conditions.
include/qpdf/Pl_Flate.hh
@@ -27,13 +27,13 @@ @@ -27,13 +27,13 @@
27 class Pl_Flate: public Pipeline 27 class Pl_Flate: public Pipeline
28 { 28 {
29 public: 29 public:
30 - static int const def_bufsize = 65536; 30 + static unsigned int const def_bufsize = 65536;
31 31
32 enum action_e { a_inflate, a_deflate }; 32 enum action_e { a_inflate, a_deflate };
33 33
34 QPDF_DLL 34 QPDF_DLL
35 Pl_Flate(char const* identifier, Pipeline* next, 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 QPDF_DLL 37 QPDF_DLL
38 virtual ~Pl_Flate(); 38 virtual ~Pl_Flate();
39 39
@@ -43,11 +43,11 @@ class Pl_Flate: public Pipeline @@ -43,11 +43,11 @@ class Pl_Flate: public Pipeline
43 virtual void finish(); 43 virtual void finish();
44 44
45 private: 45 private:
46 - void handleData(unsigned char* data, int len, int flush); 46 + void handleData(unsigned char* data, size_t len, int flush);
47 void checkError(char const* prefix, int error_code); 47 void checkError(char const* prefix, int error_code);
48 48
49 unsigned char* outbuf; 49 unsigned char* outbuf;
50 - int out_bufsize; 50 + size_t out_bufsize;
51 action_e action; 51 action_e action;
52 bool initialized; 52 bool initialized;
53 void* zdata; 53 void* zdata;
libqpdf/Pl_Flate.cc
1 #include <qpdf/Pl_Flate.hh> 1 #include <qpdf/Pl_Flate.hh>
2 #include <zlib.h> 2 #include <zlib.h>
3 #include <string.h> 3 #include <string.h>
  4 +#include <limits.h>
4 5
5 #include <qpdf/QUtil.hh> 6 #include <qpdf/QUtil.hh>
  7 +#include <qpdf/QIntC.hh>
6 8
7 Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next, 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 Pipeline(identifier, next), 11 Pipeline(identifier, next),
10 - out_bufsize(out_bufsize), 12 + out_bufsize(QIntC::to_size(out_bufsize_int)),
11 action(action), 13 action(action),
12 initialized(false) 14 initialized(false)
13 { 15 {
@@ -19,6 +21,13 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next, @@ -19,6 +21,13 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
19 // Windows environment. 21 // Windows environment.
20 this->zdata = new z_stream; 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 z_stream& zstream = *(static_cast<z_stream*>(this->zdata)); 31 z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
23 zstream.zalloc = 0; 32 zstream.zalloc = 0;
24 zstream.zfree = 0; 33 zstream.zfree = 0;
@@ -26,7 +35,7 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next, @@ -26,7 +35,7 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
26 zstream.next_in = 0; 35 zstream.next_in = 0;
27 zstream.avail_in = 0; 36 zstream.avail_in = 0;
28 zstream.next_out = this->outbuf; 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 Pl_Flate::~Pl_Flate() 41 Pl_Flate::~Pl_Flate()
@@ -77,11 +86,17 @@ Pl_Flate::write(unsigned char* data, size_t len) @@ -77,11 +86,17 @@ Pl_Flate::write(unsigned char* data, size_t len)
77 } 86 }
78 87
79 void 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 z_stream& zstream = *(static_cast<z_stream*>(this->zdata)); 97 z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
83 zstream.next_in = data; 98 zstream.next_in = data;
84 - zstream.avail_in = len; 99 + zstream.avail_in = QIntC::to_uint(len);
85 100
86 if (! this->initialized) 101 if (! this->initialized)
87 { 102 {
@@ -156,12 +171,13 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush) @@ -156,12 +171,13 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
156 // needed, so we're done for now. 171 // needed, so we're done for now.
157 done = true; 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 if (ready > 0) 176 if (ready > 0)
161 { 177 {
162 this->getNext()->write(this->outbuf, ready); 178 this->getNext()->write(this->outbuf, ready);
163 zstream.next_out = this->outbuf; 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 break; 183 break;