/src/fuzz/script_interpreter_fuzzer.c
Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <cairo.h> |
16 | | #include <cairo-script-interpreter.h> |
17 | | #include <stdint.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | static cairo_surface_t * |
22 | | _surface_create (void *closure, |
23 | | cairo_content_t content, |
24 | | double width, double height, |
25 | | long uid) |
26 | 0 | { |
27 | | // Limit size to avoid excessive memory usage |
28 | 0 | if (width <= 0 || width > 4096) width = 100; |
29 | 0 | if (height <= 0 || height > 4096) height = 100; |
30 | 0 | return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); |
31 | 0 | } |
32 | | |
33 | 4.09k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
34 | 4.09k | cairo_script_interpreter_t *csi; |
35 | 4.09k | const cairo_script_interpreter_hooks_t hooks = { |
36 | 4.09k | .surface_create = _surface_create |
37 | 4.09k | }; |
38 | | |
39 | 4.09k | if (size == 0) { |
40 | 0 | return 0; |
41 | 0 | } |
42 | | |
43 | 4.09k | csi = cairo_script_interpreter_create(); |
44 | 4.09k | if (!csi) { |
45 | 0 | return 0; |
46 | 0 | } |
47 | | |
48 | 4.09k | cairo_script_interpreter_install_hooks (csi, &hooks); |
49 | | |
50 | 4.09k | cairo_script_interpreter_feed_string(csi, (const char *)data, size); |
51 | | |
52 | 4.09k | cairo_script_interpreter_finish(csi); |
53 | 4.09k | cairo_script_interpreter_destroy(csi); |
54 | | |
55 | 4.09k | return 0; |
56 | 4.09k | } |