diff --git a/README-maintainer b/README-maintainer index f4c54324..2e11a11b 100644 --- a/README-maintainer +++ b/README-maintainer @@ -123,6 +123,12 @@ CODING RULES "Code Formatting" section in manual/contributing.rst for details. See also "CODE FORMATTING" below. +* Do not use assert in non-test code for any purpose other than as a + sanity check during development that would be safe to remove in + production. assert is for strong invariant checking. When developing + and using assert for that purpose, make sure to use the Debug + configuration since assert is disabled in other configurations. + * In a source file, include the header file that declares the source class first followed by a blank line. If a config file is needed first, put a blank line between that and the header followed by diff --git a/libqpdf/AES_PDF_native.cc b/libqpdf/AES_PDF_native.cc index 3a831efe..1316a643 100644 --- a/libqpdf/AES_PDF_native.cc +++ b/libqpdf/AES_PDF_native.cc @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include diff --git a/libqpdf/Pl_AES_PDF.cc b/libqpdf/Pl_AES_PDF.cc index 80cd8534..dc281fae 100644 --- a/libqpdf/Pl_AES_PDF.cc +++ b/libqpdf/Pl_AES_PDF.cc @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -115,7 +114,10 @@ Pl_AES_PDF::finish() // encountered files for which the output is not a // multiple of the block size. In this case, pad with // zeroes and hope for the best. - assert(this->buf_size > this->offset); + if (this->offset >= this->buf_size) { + throw std::logic_error("buffer overflow in AES encryption" + " pipeline"); + } std::memset( this->inbuf + this->offset, 0, this->buf_size - this->offset); this->offset = this->buf_size; @@ -147,7 +149,10 @@ Pl_AES_PDF::initializeVector() void Pl_AES_PDF::flush(bool strip_padding) { - assert(this->offset == this->buf_size); + if (this->offset != this->buf_size) { + throw std::logic_error( + "AES pipeline: flush called when buffer was not full"); + } if (first) { first = false; diff --git a/libqpdf/Pl_Buffer.cc b/libqpdf/Pl_Buffer.cc index b47124d3..4f45acbd 100644 --- a/libqpdf/Pl_Buffer.cc +++ b/libqpdf/Pl_Buffer.cc @@ -1,7 +1,6 @@ #include #include -#include #include #include #include diff --git a/libqpdf/Pl_LZWDecoder.cc b/libqpdf/Pl_LZWDecoder.cc index 68caa5c9..db6d7490 100644 --- a/libqpdf/Pl_LZWDecoder.cc +++ b/libqpdf/Pl_LZWDecoder.cc @@ -3,7 +3,6 @@ #include #include #include -#include #include #include diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 0b7e7c42..e5270449 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -11,18 +11,17 @@ #include #include #include -#include -#include -#include -#include - #include #include #include #include #include +#include +#include +#include #include +#include #include QPDFWriter::Members::Members(QPDF& pdf) : diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index b943777f..14b22d88 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -15,7 +15,7 @@ #include #include -#include +#include #include static unsigned char const padding_string[] = { diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index ec422406..cf88bb70 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -12,7 +12,6 @@ #include #include -#include #include #include #include @@ -172,9 +171,6 @@ QPDF::readLinearizationData() // This function throws an exception (which is trapped by // checkLinearization()) for any errors that prevent loading. - // Hint table parsing code needs at least 32 bits in a long. - assert(sizeof(long) >= 4); - if (!isLinearized()) { throw std::logic_error("called readLinearizationData for file" " that is not linearized"); diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index d22aa378..ac1bbfe6 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -6,7 +6,7 @@ #include #include #include -#include +#include QPDF::ObjUser::ObjUser() : ou_type(ou_bad), diff --git a/libqpdf/QPDF_pages.cc b/libqpdf/QPDF_pages.cc index 89a782f4..e8416b2b 100644 --- a/libqpdf/QPDF_pages.cc +++ b/libqpdf/QPDF_pages.cc @@ -1,7 +1,5 @@ #include -#include - #include #include #include @@ -233,6 +231,11 @@ QPDF::insertPage(QPDFObjectHandle newpage, int pos) QTC::TC("qpdf", "QPDF insert indirect page"); } + if ((pos < 0) || (QIntC::to_size(pos) > this->m->all_pages.size())) { + throw std::runtime_error( + "QPDF::insertPage called with pos out of range"); + } + QTC::TC( "qpdf", "QPDF insert page", @@ -249,7 +252,6 @@ QPDF::insertPage(QPDFObjectHandle newpage, int pos) QPDFObjectHandle pages = getRoot().getKey("/Pages"); QPDFObjectHandle kids = pages.getKey("/Kids"); - assert((pos >= 0) && (QIntC::to_size(pos) <= this->m->all_pages.size())); newpage.replaceKey("/Parent", pages); kids.insertItem(pos, newpage);