Debian Codegraph @

compat/libpng/contrib/oss-fuzz/libpng_colormap_fuzzer.cc

raw
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <setjmp.h>
5 #include <png.h>
6
7 /* Test png_image API and colormap functions */
8 static void test_png_image_colormap(const uint8_t *data, size_t size) {
9 png_image image;
10 memset(&image, 0, sizeof(image));
11 image.version = PNG_IMAGE_VERSION;
12
13 if (!png_image_begin_read_from_memory(&image, data, size)) {
14 return;
15 }
16
17 /* Limit image size for performance */
18 if (image.width > 1024 || image.height > 1024) {
19 png_image_free(&image);
20 return;
21 }
22
23 /* Test different colormap formats to trigger uncovered functions */
24 png_uint_32 formats[] = {
25 PNG_FORMAT_GRAY,
26 PNG_FORMAT_GA,
27 PNG_FORMAT_RGB,
28 PNG_FORMAT_RGBA,
29 PNG_FORMAT_RGB_COLORMAP,
30 PNG_FORMAT_RGBA_COLORMAP
31 };
32
33 int i;
34 for (i = 0; i < 6; i++) {
35 image.format = formats[i];
36
37 size_t buffer_size = PNG_IMAGE_SIZE(image);
38 if (buffer_size > 0 && buffer_size < 5*1024*1024) { /* Limit to 5MB */
39 void *img_buffer = malloc(buffer_size);
40 if (img_buffer) {
41 if (image.format & PNG_FORMAT_FLAG_COLORMAP) {
42 png_bytep colormap = (png_bytep)malloc(PNG_IMAGE_COLORMAP_SIZE(image));
43 if (colormap) {
44 /* This triggers png_image_finish_read and colormap generation */
45 png_image_finish_read(&image, NULL, img_buffer, 0, colormap);
46 free(colormap);
47 }
48 } else {
49 png_image_finish_read(&image, NULL, img_buffer, 0, NULL);
50 }
51 free(img_buffer);
52 }
53 }
54
55 /* Reset for next format */
56 if (i < 5) {
57 png_image_free(&image);
58 memset(&image, 0, sizeof(image));
59 image.version = PNG_IMAGE_VERSION;
60 if (!png_image_begin_read_from_memory(&image, data, size)) {
61 break;
62 }
63 }
64 }
65
66 png_image_free(&image);
67 }
68
69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
70 if (size < 8) return 0;
71
72 /* Test 2: PNG image API with colormap processing */
73 test_png_image_colormap(data, size);
74
75 return 0;
76 }