/src/poppler/glib/tests/fuzzing/find_text_fuzzer.cc
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | #include <stdint.h> |
3 | | #include <string.h> |
4 | | #include <poppler.h> |
5 | | |
6 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
7 | 11.3k | { |
8 | 11.3k | GError *err = NULL; |
9 | 11.3k | PopplerDocument *doc; |
10 | 11.3k | PopplerPage *page; |
11 | 11.3k | char *buf; |
12 | 11.3k | int npages; |
13 | 11.3k | GList *matches; |
14 | | |
15 | 11.3k | doc = poppler_document_new_from_data((char *)data, size, NULL, &err); |
16 | 11.3k | if (doc == NULL) { |
17 | 3.69k | g_error_free(err); |
18 | 3.69k | return 0; |
19 | 3.69k | } |
20 | | |
21 | 7.62k | npages = poppler_document_get_n_pages(doc); |
22 | 7.62k | if (npages < 1) { |
23 | 807 | g_object_unref(doc); |
24 | 807 | return 0; |
25 | 807 | } |
26 | | |
27 | 6.81k | buf = (char *)calloc(size + 1, sizeof(char)); |
28 | 6.81k | memcpy(buf, data, size); |
29 | 6.81k | buf[size] = '\0'; |
30 | | |
31 | 98.9k | for (int n = 0; n < npages; n++) { |
32 | 92.1k | page = poppler_document_get_page(doc, n); |
33 | 92.1k | if (!page) { |
34 | 81.2k | continue; |
35 | 81.2k | } |
36 | 10.8k | if (g_utf8_validate(buf, -1, NULL)) { |
37 | 9.77k | matches = poppler_page_find_text(page, buf); |
38 | 9.77k | if (matches) { |
39 | 285 | g_list_free_full(matches, (GDestroyNotify)poppler_rectangle_free); |
40 | 285 | } |
41 | 9.77k | } |
42 | 10.8k | g_object_unref(page); |
43 | 10.8k | } |
44 | 6.81k | free(buf); |
45 | 6.81k | g_object_unref(doc); |
46 | 6.81k | return 0; |
47 | 7.62k | } |