/src/fuzz/text_glyphs_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 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 "fuzzer_temp_file.h" |
17 | | |
18 | | const int glyph_range = 9; |
19 | | |
20 | 31 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
21 | 31 | if (size < glyph_range) { |
22 | 0 | return 0; |
23 | 0 | } |
24 | 31 | cairo_t *cr; |
25 | 31 | cairo_surface_t *surface; |
26 | 31 | cairo_status_t status; |
27 | 31 | cairo_text_extents_t extents; |
28 | 31 | cairo_text_cluster_t cluster; |
29 | | |
30 | 31 | char *tmpfile = fuzzer_get_tmpfile(data, size); |
31 | 31 | surface = cairo_image_surface_create_from_png(tmpfile); |
32 | 31 | status = cairo_surface_status(surface); |
33 | 31 | if (status != CAIRO_STATUS_SUCCESS) { |
34 | 25 | fuzzer_release_tmpfile(tmpfile); |
35 | 25 | return 0; |
36 | 25 | } |
37 | | |
38 | 6 | char *buf = (char *) calloc(size + 1, sizeof(char)); |
39 | 6 | memcpy(buf, data, size); |
40 | 6 | buf[size] = '\0'; |
41 | | |
42 | 6 | cr = cairo_create(surface); |
43 | 6 | cairo_text_extents(cr, buf, &extents); |
44 | 6 | cluster.num_bytes = size; |
45 | 6 | cluster.num_glyphs = 1; |
46 | 60 | for (int i = 0; i < glyph_range; i++) { |
47 | | // Taken from test/text-glyph-range.c |
48 | 54 | cairo_glyph_t glyph = { |
49 | 54 | (long int)data[i], 10 * i, 25 |
50 | 54 | }; |
51 | 54 | cairo_show_text_glyphs(cr, buf, size, &glyph, 1, &cluster, 1, 0); |
52 | 54 | } |
53 | | |
54 | 6 | cairo_destroy(cr); |
55 | 6 | cairo_surface_destroy(surface); |
56 | 6 | free(buf); |
57 | 6 | fuzzer_release_tmpfile(tmpfile); |
58 | 6 | return 0; |
59 | 31 | } |