diff --git a/src/errors.cpp b/src/errors.cpp index bfcbd2f..2dd9bf7 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -34,6 +34,7 @@ const std::array error_texts{ "Array has zero length.", "Could not open file.", "Writing to file failed.", +"Required argument is NULL.", }; // clang-format on diff --git a/src/errors.hpp b/src/errors.hpp index abf0af1..ee75787 100644 --- a/src/errors.hpp +++ b/src/errors.hpp @@ -35,6 +35,7 @@ enum class ErrorCode : int32_t { ZeroLengthArray, CouldNotOpenFile, FileWriteError, + ArgIsNull, // When you add an error code here, also add the string representation in the .cpp file. NumErrors, }; diff --git a/src/pdfcapi.cpp b/src/pdfcapi.cpp index 83eecde..9d64320 100644 --- a/src/pdfcapi.cpp +++ b/src/pdfcapi.cpp @@ -21,6 +21,11 @@ using namespace A4PDF; +#define CHECK_NULL(x) \ + if(x == nullptr) { \ + return (A4PDF_EC)ErrorCode::ArgIsNull; \ + } + A4PDF_EC a4pdf_options_new(A4PDF_Options **out_ptr) A4PDF_NOEXCEPT { *out_ptr = reinterpret_cast(new PdfGenerationData()); return (A4PDF_EC)ErrorCode::NoError; @@ -49,6 +54,9 @@ A4PDF_PUBLIC A4PDF_EC a4pdf_options_set_mediabox( A4PDF_EC a4pdf_generator_new(const char *filename, const A4PDF_Options *options, A4PDF_Generator **out_ptr) A4PDF_NOEXCEPT { + CHECK_NULL(filename); + CHECK_NULL(options); + CHECK_NULL(out_ptr); auto opts = reinterpret_cast(options); *out_ptr = reinterpret_cast(new PdfGen(filename, *opts)); return (A4PDF_EC)ErrorCode::NoError;