diff -rupN podofo-0.9.6/src/base/PdfDictionary.h podofo-0.9.6-new/src/base/PdfDictionary.h --- podofo-0.9.6/src/base/PdfDictionary.h 2018-06-11 01:46:56.000000000 +0200 +++ podofo-0.9.6-new/src/base/PdfDictionary.h 2019-03-13 23:15:12.260138718 +0100 @@ -180,6 +180,21 @@ class PODOFO_API PdfDictionary : public */ PdfObject* GetKey( const PdfName & key ); + /** Get the key's value out of the dictionary. + * + * The returned value is a reference to the internal object in the dictionary + * so it MUST not be deleted. If the key is not found, this throws a PdfError + * exception with error code ePdfError_NoObject, instead of returning. + * This is intended to make code more readable by sparing (especially multiple) + * NULL checks. + * + * \param key look for the key named key in the dictionary + * + * \returns reference to the found value (never 0). + * \throws PdfError(ePdfError_NoObject). + */ + inline const PdfObject& MustGetKey( const PdfName & key ) const; + pdf_int64 GetKeyAsLong( const PdfName & key, pdf_int64 lDefault = 0 ) const; double GetKeyAsReal( const PdfName & key, double dDefault = 0.0 ) const; @@ -305,6 +320,18 @@ TKeyMap & PdfDictionary::GetKeys() // ----------------------------------------------------- // // ----------------------------------------------------- + +const PdfObject& PdfDictionary::MustGetKey( const PdfName & key ) const +{ + const PdfObject* obj = GetKey( key ); + if (!obj) + PODOFO_RAISE_ERROR( ePdfError_NoObject ); + return *obj; +} + +// ----------------------------------------------------- +// +// ----------------------------------------------------- void PdfDictionary::Write( PdfOutputDevice* pDevice, EPdfWriteMode eWriteMode, const PdfEncrypt* pEncrypt ) const { this->Write( pDevice, eWriteMode, pEncrypt, PdfName::KeyNull ); diff -rupN podofo-0.9.6/src/base/PdfEncrypt.cpp podofo-0.9.6-new/src/base/PdfEncrypt.cpp --- podofo-0.9.6/src/base/PdfEncrypt.cpp 2017-02-26 21:48:19.000000000 +0100 +++ podofo-0.9.6-new/src/base/PdfEncrypt.cpp 2019-03-13 23:15:12.260138718 +0100 @@ -561,13 +561,13 @@ PdfEncrypt* PdfEncrypt::CreatePdfEncrypt try { PdfString sTmp; - lV = static_cast(pObject->GetDictionary().GetKey( PdfName("V") )->GetNumber()); - rValue = static_cast(pObject->GetDictionary().GetKey( PdfName("R") )->GetNumber()); + lV = static_cast(pObject->GetDictionary().MustGetKey( PdfName("V") ).GetNumber()); + rValue = static_cast( pObject->GetDictionary().MustGetKey( PdfName("R") ).GetNumber()); - pValue = static_cast(pObject->GetDictionary().GetKey( PdfName("P") )->GetNumber()); + pValue = static_cast( pObject->GetDictionary().MustGetKey( PdfName("P") ).GetNumber()); - oValue = pObject->GetDictionary().GetKey( PdfName("O") )->GetString(); - uValue = pObject->GetDictionary().GetKey( PdfName("U") )->GetString(); + oValue = pObject->GetDictionary().MustGetKey( PdfName("O") ).GetString(); + uValue = pObject->GetDictionary().MustGetKey( PdfName("U") ).GetString(); if( pObject->GetDictionary().HasKey( PdfName("Length") ) ) { @@ -593,7 +593,7 @@ PdfEncrypt* PdfEncrypt::CreatePdfEncrypt } } } catch( PdfError & e ) { - e.AddToCallstack( __FILE__, __LINE__, "Invalid key in encryption dictionary" ); + e.AddToCallstack( __FILE__, __LINE__, "Invalid or missing key in encryption dictionary" ); throw e; }