From fe1fffe8dba0926b7a738ed2195590b7db18dcab Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 18 Jul 2024 10:41:15 +0100 Subject: [PATCH] Change QPDF max_warnings into a hard limit --- fuzz/qpdf_fuzzer.cc | 2 +- include/qpdf/QPDF.hh | 6 +++--- libqpdf/QPDF.cc | 10 ++++------ libqpdf/QPDF_json.cc | 19 +++++++------------ 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/fuzz/qpdf_fuzzer.cc b/fuzz/qpdf_fuzzer.cc index 20073d2..db33b2c 100644 --- a/fuzz/qpdf_fuzzer.cc +++ b/fuzz/qpdf_fuzzer.cc @@ -57,7 +57,7 @@ FuzzHelper::getQpdf() auto is = std::shared_ptr(new BufferInputSource("fuzz input", &this->input_buffer)); auto qpdf = QPDF::create(); - qpdf->setMaxWarnings(20); + qpdf->setMaxWarnings(500); qpdf->processInputSource(is); return qpdf; } diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index a57925a..a79c0dc 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -228,9 +228,9 @@ class QPDF QPDF_DLL void setSuppressWarnings(bool); - // Set the maximum number of warnings to output. Subsequent warnings are suppressed. + // Set the maximum number of warnings. A QPDFExc is thrown if the limit is exceeded. QPDF_DLL - void setMaxWarnings(int); + void setMaxWarnings(size_t); // 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 @@ -1501,7 +1501,7 @@ class QPDF bool provided_password_is_hex_key{false}; bool ignore_xref_streams{false}; bool suppress_warnings{false}; - int max_warnings{0}; + size_t 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 88373cb..768dc5f 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -332,7 +332,7 @@ QPDF::setSuppressWarnings(bool val) } void -QPDF::setMaxWarnings(int val) +QPDF::setMaxWarnings(size_t val) { m->max_warnings = val; } @@ -504,13 +504,11 @@ QPDF::inParse(bool v) void QPDF::warn(QPDFExc const& e) { + if (m->max_warnings > 0 && m->warnings.size() >= m->max_warnings) { + stopOnError("Too many warnings - file is too badly damaged"); + } m->warnings.push_back(e); if (!m->suppress_warnings) { - 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; - } *m->log->getWarn() << "WARNING: " << m->warnings.back().what() << "\n"; } } diff --git a/libqpdf/QPDF_json.cc b/libqpdf/QPDF_json.cc index ffdd711..30aca25 100644 --- a/libqpdf/QPDF_json.cc +++ b/libqpdf/QPDF_json.cc @@ -233,13 +233,12 @@ provide_data(std::shared_ptr is, qpdf_offset_t start, qpdf_offset_t class QPDF::JSONReactor: public JSON::Reactor { public: - JSONReactor(QPDF& pdf, std::shared_ptr is, bool must_be_complete, int max_warnings) : + JSONReactor(QPDF& pdf, std::shared_ptr is, bool must_be_complete) : pdf(pdf), is(is), must_be_complete(must_be_complete), descr(std::make_shared( - QPDFValue::JSON_Descr(std::make_shared(is->getName()), ""))), - max_warnings(max_warnings) + QPDFValue::JSON_Descr(std::make_shared(is->getName()), ""))) { for (auto& oc: pdf.m->obj_cache) { if (oc.second.object->getTypeCode() == ::ot_reserved) { @@ -292,8 +291,7 @@ class QPDF::JSONReactor: public JSON::Reactor std::shared_ptr is; bool must_be_complete{true}; std::shared_ptr descr; - int errors{0}; - int max_warnings{0}; + bool errors{false}; bool saw_qpdf{false}; bool saw_qpdf_meta{false}; bool saw_objects{false}; @@ -316,21 +314,18 @@ class QPDF::JSONReactor: public JSON::Reactor void QPDF::JSONReactor::error(qpdf_offset_t offset, std::string const& msg) { - ++errors; + errors = true; std::string object = this->cur_object; if (is->getName() != pdf.getFilename()) { object += " from " + is->getName(); } - this->pdf.warn(qpdf_e_json, object, offset, msg); - if (max_warnings > 0 && errors >= max_warnings) { - throw std::runtime_error("errors found in JSON"); - } + pdf.warn(qpdf_e_json, object, offset, msg); } bool QPDF::JSONReactor::anyErrors() const { - return errors > 0; + return errors; } void @@ -825,7 +820,7 @@ QPDF::updateFromJSON(std::shared_ptr is) void QPDF::importJSON(std::shared_ptr is, bool must_be_complete) { - JSONReactor reactor(*this, is, must_be_complete, m->max_warnings); + JSONReactor reactor(*this, is, must_be_complete); try { JSON::parse(*is, &reactor); } catch (std::runtime_error& e) { -- libgit2 0.21.4