#include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

#include "utils.cc"

#define FUZZ_ENCODER_STRING_LITERAL_X(name) FUZZ_ENCODER_STRING_LITERAL(name)
#define FUZZ_ENCODER_STRING_LITERAL(name) #name
#ifndef FUZZ_ENCODER
#define FUZZ_ENCODER FUZZ_ENCODER_STRING_LITERAL_X(FUZZ_IMAGEMAGICK_ENCODER)
#endif

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  std::string encoder = FUZZ_ENCODER;
  // Due to macro foolishness, if we pass -DFUZZ_IMAGEMAGICK_ENCODER=NULL,
  // FUZZ_ENCODER ends up being "__null".
  if (encoder == "__null") {
    encoder = "NULL";
  }
  
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  image.magick(encoder);
  image.fileName(std::string(encoder) + ":");
  try {
    image.read(blob);
  }
  catch (Magick::Exception &e) {
    return 0;
  }

#if FUZZ_IMAGEMAGICK_ENCODER_WRITE || BUILD_MAIN

  Magick::Blob outBlob;
  try {
    image.write(&outBlob, encoder);
  }
  catch (Magick::Exception &e) {
  }
#endif
  return 0;
}

#include "travis.cc"

#include <cstdint>

#include <Magick++/Blob.h>
#include <Magick++/Image.h>

#include "utils.cc"

#define FUZZ_ENCODER_STRING_LITERAL_X(name) FUZZ_ENCODER_STRING_LITERAL(name)
#define FUZZ_ENCODER_STRING_LITERAL(name) #name
#ifndef FUZZ_ENCODER
#define FUZZ_ENCODER FUZZ_ENCODER_STRING_LITERAL_X(FUZZ_IMAGEMAGICK_ENCODER)
#endif

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  const Magick::Blob blob(Data, Size);
  Magick::Image image;
  image.magick(FUZZ_ENCODER);
  image.fileName(std::string(FUZZ_ENCODER) + ":");
  try {
    image.read(blob);
  }
  catch (Magick::Exception &e) {
    return 0;
  }

#if FUZZ_IMAGEMAGICK_ENCODER_WRITE || BUILD_MAIN

  Magick::Blob outBlob;
  try {
    image.write(&outBlob, FUZZ_ENCODER);
  }
  catch (Magick::Exception &e) {
  }
#endif
  return 0;
}

#include "travis.cc"

--- 
+++ 
@@ -12,10 +12,17 @@
 #endif
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+  std::string encoder = FUZZ_ENCODER;
+  // Due to macro foolishness, if we pass -DFUZZ_IMAGEMAGICK_ENCODER=NULL,
+  // FUZZ_ENCODER ends up being "__null".
+  if (encoder == "__null") {
+    encoder = "NULL";
+  }
+  
   const Magick::Blob blob(Data, Size);
   Magick::Image image;
-  image.magick(FUZZ_ENCODER);
-  image.fileName(std::string(FUZZ_ENCODER) + ":");
+  image.magick(encoder);
+  image.fileName(std::string(encoder) + ":");
   try {
     image.read(blob);
   }
@@ -27,7 +34,7 @@
 
   Magick::Blob outBlob;
   try {
-    image.write(&outBlob, FUZZ_ENCODER);
+    image.write(&outBlob, encoder);
   }
   catch (Magick::Exception &e) {
   }
Handle the null encoder properly in the encoder fuzz

Credit OSS-Fuzz