pix_rotate_shear_fuzzer.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // The fuzzer takes as input a buffer of bytes. The buffer is read in as:
  2. // <angle>, <x_center>, <y_center>, and the remaining bytes will be read
  3. // in as a <pix>. The image is then rotated by angle around the center. All
  4. // inputs should not result in undefined behavior.
  5. #include <cmath>
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include "leptfuzz.h"
  11. // Set to true only for debugging; always false for production
  12. static const bool DebugOutput = false;
  13. namespace {
  14. // Reads the front bytes of a data buffer containing `size` bytes as an int16_t,
  15. // and advances the buffer forward [if there is sufficient capacity]. If there
  16. // is insufficient capacity, this returns 0 and does not modify size or data.
  17. int16_t ReadInt16(const uint8_t** data, size_t* size) {
  18. int16_t result = 0;
  19. if (*size >= sizeof(result)) {
  20. memcpy(&result, *data, sizeof(result));
  21. *data += sizeof(result);
  22. *size -= sizeof(result);
  23. }
  24. return result;
  25. }
  26. } // namespace
  27. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  28. const int16_t angle = ReadInt16(&data, &size);
  29. const int16_t x_center = ReadInt16(&data, &size);
  30. const int16_t y_center = ReadInt16(&data, &size);
  31. leptSetStdNullHandler();
  32. // Don't do pnm format (which can cause timeouts) or
  33. // jpeg format (which can have uninitialized variables.
  34. // The format checker requires at least 12 bytes.
  35. if (size < 12) return EXIT_SUCCESS;
  36. int format;
  37. findFileFormatBuffer(data, &format);
  38. if (format == IFF_PNM || format == IFF_JFIF_JPEG ||
  39. format == IFF_TIFF) return EXIT_SUCCESS;
  40. Pix* pix = pixReadMem(reinterpret_cast<const unsigned char*>(data), size);
  41. if (pix == nullptr) {
  42. return EXIT_SUCCESS;
  43. }
  44. // Never in production
  45. if (DebugOutput) {
  46. L_INFO("w = %d, h = %d, d = %d\n", "fuzzer",
  47. pixGetWidth(pix), pixGetHeight(pix), pixGetDepth(pix));
  48. }
  49. constexpr float deg2rad = M_PI / 180.;
  50. Pix* pix_rotated = pixRotateShear(pix, x_center, y_center, deg2rad * angle,
  51. L_BRING_IN_WHITE);
  52. if (pix_rotated) {
  53. pixDestroy(&pix_rotated);
  54. }
  55. pixDestroy(&pix);
  56. return EXIT_SUCCESS;
  57. }