/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 | 12.2k | { |
8 | 12.2k | GError *err = NULL; |
9 | 12.2k | PopplerDocument *doc; |
10 | 12.2k | PopplerPage *page; |
11 | 12.2k | char *buf; |
12 | 12.2k | int npages; |
13 | 12.2k | GList *matches; |
14 | | |
15 | 12.2k | doc = poppler_document_new_from_data((char *)data, size, NULL, &err); |
16 | 12.2k | if (doc == NULL) { |
17 | 3.99k | g_error_free(err); |
18 | 3.99k | return 0; |
19 | 3.99k | } |
20 | | |
21 | 8.23k | npages = poppler_document_get_n_pages(doc); |
22 | 8.23k | if (npages < 1) { |
23 | 760 | g_object_unref(doc); |
24 | 760 | return 0; |
25 | 760 | } |
26 | | |
27 | 7.47k | buf = (char *)calloc(size + 1, sizeof(char)); |
28 | 7.47k | memcpy(buf, data, size); |
29 | 7.47k | buf[size] = '\0'; |
30 | | |
31 | 93.8k | for (int n = 0; n < npages; n++) { |
32 | 86.3k | page = poppler_document_get_page(doc, n); |
33 | 86.3k | if (!page) { |
34 | 74.8k | continue; |
35 | 74.8k | } |
36 | 11.4k | if (g_utf8_validate(buf, -1, NULL)) { |
37 | 10.3k | matches = poppler_page_find_text(page, buf); |
38 | 10.3k | if (matches) { |
39 | 336 | g_list_free_full(matches, (GDestroyNotify)poppler_rectangle_free); |
40 | 336 | } |
41 | 10.3k | } |
42 | 11.4k | g_object_unref(page); |
43 | 11.4k | } |
44 | 7.47k | free(buf); |
45 | 7.47k | g_object_unref(doc); |
46 | 7.47k | return 0; |
47 | 8.23k | } |