Commit 6a7d53ad2bc9c7eb5964b5c0424616e8b3f068cd

Authored by Jay Berkenbilt
1 parent 07d6f770

Handle zlib data errors better (fixes #106)

ChangeLog
1 1 2017-07-29 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * Handle zlib data errors when decoding streams. Fixes #106.
  4 +
3 5 * Improve handling of files where the "stream" keyword is not
4 6 followed by proper line terminators. Fixes #104.
5 7  
... ...
libqpdf/Pl_Flate.cc
... ... @@ -157,28 +157,36 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
157 157 void
158 158 Pl_Flate::finish()
159 159 {
160   - if (this->outbuf)
  160 + try
161 161 {
162   - if (this->initialized)
163   - {
164   - z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
165   - unsigned char buf[1];
166   - buf[0] = '\0';
167   - handleData(buf, 0, Z_FINISH);
168   - int err = Z_OK;
169   - if (action == a_deflate)
170   - {
171   - err = deflateEnd(&zstream);
172   - }
173   - else
174   - {
175   - err = inflateEnd(&zstream);
176   - }
177   - checkError("End", err);
178   - }
179   -
180   - delete [] this->outbuf;
181   - this->outbuf = 0;
  162 + if (this->outbuf)
  163 + {
  164 + if (this->initialized)
  165 + {
  166 + z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
  167 + unsigned char buf[1];
  168 + buf[0] = '\0';
  169 + handleData(buf, 0, Z_FINISH);
  170 + int err = Z_OK;
  171 + if (action == a_deflate)
  172 + {
  173 + err = deflateEnd(&zstream);
  174 + }
  175 + else
  176 + {
  177 + err = inflateEnd(&zstream);
  178 + }
  179 + checkError("End", err);
  180 + }
  181 +
  182 + delete [] this->outbuf;
  183 + this->outbuf = 0;
  184 + }
  185 + }
  186 + catch (std::exception& e)
  187 + {
  188 + this->getNext()->finish();
  189 + throw e;
182 190 }
183 191 this->getNext()->finish();
184 192 }
... ...
libqpdf/QPDF.cc
... ... @@ -2206,7 +2206,7 @@ QPDF::pipeStreamData(int objid, int generation,
2206 2206 warn(e);
2207 2207 }
2208 2208 }
2209   - catch (std::runtime_error& e)
  2209 + catch (std::exception& e)
2210 2210 {
2211 2211 if (! suppress_warnings)
2212 2212 {
... ... @@ -2218,7 +2218,14 @@ QPDF::pipeStreamData(int objid, int generation,
2218 2218 QUtil::int_to_string(generation) + ": " + e.what()));
2219 2219 }
2220 2220 }
2221   - pipeline->finish();
  2221 + try
  2222 + {
  2223 + pipeline->finish();
  2224 + }
  2225 + catch (std::exception&)
  2226 + {
  2227 + // ignore
  2228 + }
2222 2229 return success;
2223 2230 }
2224 2231  
... ...
qpdf/qtest/qpdf.test
... ... @@ -206,7 +206,7 @@ $td-&gt;runtest(&quot;remove page we don&#39;t have&quot;,
206 206 show_ntests();
207 207 # ----------
208 208 $td->notify("--- Miscellaneous Tests ---");
209   -$n_tests += 88;
  209 +$n_tests += 89;
210 210  
211 211 $td->runtest("qpdf version",
212 212 {$td->COMMAND => "qpdf --version"},
... ... @@ -229,6 +229,7 @@ foreach my $d (
229 229 ["118", "other infinite loop", 2],
230 230 ["119", "other infinite loop", 3],
231 231 ["120", "other infinite loop", 3],
  232 + ["106", "zlib data error", 3],
232 233 )
233 234 {
234 235 my ($n, $description, $exit_status) = @$d;
... ...
qpdf/qtest/qpdf/issue-106.out 0 → 100644
  1 +WARNING: issue-106.pdf (file position 56627): error decoding stream data for object 29 0: stream inflate: inflate: data: incorrect data check
  2 +WARNING: issue-106.pdf (file position 64303): error decoding stream data for object 30 0: stream inflate: inflate: data: incorrect data check
  3 +WARNING: issue-106.pdf (file position 67427): error decoding stream data for object 31 0: stream inflate: inflate: data: incorrect data check
  4 +qpdf: operation succeeded with warnings; resulting file may have some problems
... ...
qpdf/qtest/qpdf/issue-106.pdf 0 → 100644
No preview for this file type