Skip to content

Commit

Permalink
Guard against some null pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpakkane committed Mar 12, 2023
1 parent 94ae77f commit e314c59
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/errors.cpp
Expand Up @@ -34,6 +34,7 @@ const std::array<const char *, (std::size_t)ErrorCode::NumErrors> error_texts{
"Array has zero length.",
"Could not open file.",
"Writing to file failed.",
"Required argument is NULL.",
};

// clang-format on
Expand Down
1 change: 1 addition & 0 deletions src/errors.hpp
Expand Up @@ -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,
};
Expand Down
8 changes: 8 additions & 0 deletions src/pdfcapi.cpp
Expand Up @@ -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<A4PDF_Options *>(new PdfGenerationData());
return (A4PDF_EC)ErrorCode::NoError;
Expand Down Expand Up @@ -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<const PdfGenerationData *>(options);
*out_ptr = reinterpret_cast<A4PDF_Generator *>(new PdfGen(filename, *opts));
return (A4PDF_EC)ErrorCode::NoError;
Expand Down

0 comments on commit e314c59

Please sign in to comment.