/src/liblouis/tests/fuzzing/fuzz_translate_generic.c
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // liblouis Braille Translation and Back-Translation Library |
3 | | // |
4 | | // Copyright (C) 2022 Anna Stan, Nicolas Morel, Kalilou Mamadou Dramé |
5 | | // |
6 | | // This file is part of liblouis. |
7 | | // |
8 | | // liblouis is free software: you can redistribute it and/or modify it |
9 | | // under the terms of the GNU Lesser General Public License as published |
10 | | // by the Free Software Foundation, either version 2.1 of the License, or |
11 | | // (at your option) any later version. |
12 | | // |
13 | | // liblouis is distributed in the hope that it will be useful, but |
14 | | // WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | // Lesser General Public License for more details. |
17 | | // |
18 | | // You should have received a copy of the GNU Lesser General Public |
19 | | // License along with liblouis. If not, see <http://www.gnu.org/licenses/>. |
20 | | // |
21 | | |
22 | | #include <config.h> |
23 | | |
24 | | #include <stdlib.h> |
25 | | #include <stdint.h> |
26 | | #include <stdio.h> |
27 | | #include <string.h> |
28 | | |
29 | | #include <internal.h> |
30 | | #include <liblouis.h> |
31 | | #include <assert.h> |
32 | | #include <unistd.h> |
33 | | |
34 | | #define LANGUAGE "en" |
35 | | |
36 | | static int initialized = 0; |
37 | | |
38 | | #define BOLDRED(x) "\x1b[31m\x1b[1m" x "\x1b[0m" |
39 | | |
40 | | static const char *table_default; |
41 | | |
42 | | static void __attribute__((destructor)) |
43 | 0 | free_ressources(void) { |
44 | 0 | lou_free(); |
45 | 0 | } |
46 | | |
47 | | void |
48 | 0 | avoid_log(logLevels level, const char *msg) { |
49 | 0 | (void) level; |
50 | 0 | (void) msg; |
51 | 0 | } |
52 | | |
53 | | extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
54 | | |
55 | | int |
56 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
57 | | int inputLen = 0; |
58 | | int outputLen = 0; |
59 | | static int counter = 0; |
60 | | if (!initialized) |
61 | | { |
62 | | lou_registerLogCallback(avoid_log); |
63 | | initialized = 1; |
64 | | } |
65 | | |
66 | | if (size < 512) { |
67 | | return 0; |
68 | | } |
69 | | |
70 | | // Write first half of fuzz data to a table file. |
71 | | char new_file[256]; |
72 | | sprintf(new_file, "/tmp/libfuzzer-%d.ctb", counter); |
73 | | counter++; |
74 | | |
75 | | FILE *fp = fopen(new_file, "wb"); |
76 | | if (!fp) { |
77 | | return 0; |
78 | | } |
79 | | fwrite(data, 512, 1, fp); |
80 | | fclose(fp); |
81 | | |
82 | | // Adjust data pointer to after the table file data. |
83 | | data += 512; |
84 | | size -= 512; |
85 | | |
86 | | /* check if this works, otherwise bail */ |
87 | | if (lou_checkTable(new_file) == 0) { |
88 | | lou_free(); |
89 | | unlink(new_file); |
90 | | return 0; |
91 | | } |
92 | | char *mutable_data = NULL; |
93 | | table_default = new_file; |
94 | | |
95 | | mutable_data = strndup((char*)data, size); |
96 | | if (!mutable_data) |
97 | | { |
98 | | perror("malloc"); |
99 | | exit(1); |
100 | | } |
101 | | |
102 | | widechar *inputText = malloc((size*16+1)*sizeof(widechar)); |
103 | | int len = (int)_lou_extParseChars(mutable_data, inputText); |
104 | | free(mutable_data); |
105 | | if (len <= 0) { |
106 | | free(inputText); |
107 | | unlink(new_file); |
108 | | lou_free(); |
109 | | return -1; |
110 | | } |
111 | | |
112 | | assert(len <= (size*16)); |
113 | | inputLen = len; |
114 | | outputLen = len*16; |
115 | | widechar *outputText = malloc((outputLen+1)*sizeof(widechar)); |
116 | | |
117 | | lou_translateString(table_default, inputText, &inputLen, outputText, &outputLen, NULL, NULL, ucBrl); |
118 | | |
119 | | free(inputText); |
120 | | free(outputText); |
121 | | lou_free(); |
122 | | unlink(new_file); |
123 | | |
124 | | return 0; |
125 | | } |