From 598268f6ade9fef4e18af211e77eb4e28c1ce2dc Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 2 Jul 2024 09:14:21 -0400 Subject: [PATCH] Add setMaxWarnings rather than using conditional compilation --- ChangeLog | 11 +++++++++++ fuzz/qpdf_fuzzer.cc | 1 + include/qpdf/QPDF.hh | 5 +++++ libqpdf/QPDF.cc | 13 ++++++++----- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4d6b8b5e..1eb89d74 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-07-02 Jay Berkenbilt + + * Add QPDF::setMaxWarnings to set the maximum of warnings before + warning suppression. + + * Add additional options to Pl_DCT construction to limit sizes and + memory usage of compression. These are generally exposed but are + primarily intended to support fuzz tests, which have explicit + memory limits that are smaller than what is commonly seen in the + wild with PDF files. + 2024-06-07 Jay Berkenbilt * 11.9.1: release diff --git a/fuzz/qpdf_fuzzer.cc b/fuzz/qpdf_fuzzer.cc index d676e8cb..20073d28 100644 --- a/fuzz/qpdf_fuzzer.cc +++ b/fuzz/qpdf_fuzzer.cc @@ -57,6 +57,7 @@ FuzzHelper::getQpdf() auto is = std::shared_ptr(new BufferInputSource("fuzz input", &this->input_buffer)); auto qpdf = QPDF::create(); + qpdf->setMaxWarnings(20); qpdf->processInputSource(is); return qpdf; } diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index c83bcb0b..0044e58a 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -228,6 +228,10 @@ class QPDF QPDF_DLL void setSuppressWarnings(bool); + // Set the maximum number of warnings to output. Subsequent warnings are suppressed. + QPDF_DLL + void setMaxWarnings(int); + // By default, QPDF will try to recover if it finds certain types of errors in PDF files. If // turned off, it will throw an exception on the first such problem it finds without attempting // recovery. @@ -1497,6 +1501,7 @@ class QPDF bool provided_password_is_hex_key{false}; bool ignore_xref_streams{false}; bool suppress_warnings{false}; + int max_warnings{0}; bool attempt_recovery{true}; bool check_mode{false}; std::shared_ptr encp; diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index 52fc1ce2..af2811fc 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -331,6 +331,12 @@ QPDF::setSuppressWarnings(bool val) m->suppress_warnings = val; } +void +QPDF::setMaxWarnings(int val) +{ + m->suppress_warnings = val; +} + void QPDF::setAttemptRecovery(bool val) { @@ -500,14 +506,11 @@ QPDF::warn(QPDFExc const& e) { m->warnings.push_back(e); if (!m->suppress_warnings) { -// QXXXQ -#ifdef QPDF_OSS_FUZZ - if (m->warnings.size() > 20) { - *m->log->getWarn() << "WARNING: too many warnings - additional warnings surpressed\n"; + if (m->max_warnings > 0 && m->warnings.size() > 20) { + *m->log->getWarn() << "WARNING: too many warnings - additional warnings suppressed\n"; m->suppress_warnings = true; return; } -#endif *m->log->getWarn() << "WARNING: " << m->warnings.back().what() << "\n"; } }