/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 | 44.1k | avoid_log(logLevels level, const char *msg) { |
49 | 44.1k | (void) level; |
50 | 44.1k | (void) msg; |
51 | 44.1k | } |
52 | | |
53 | | extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
54 | | |
55 | | int |
56 | 95 | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
57 | 95 | int inputLen = 0; |
58 | 95 | int outputLen = 0; |
59 | 95 | static int counter = 0; |
60 | 95 | if (!initialized) |
61 | 2 | { |
62 | 2 | lou_registerLogCallback(avoid_log); |
63 | 2 | initialized = 1; |
64 | 2 | } |
65 | | |
66 | 95 | if (size < 512) { |
67 | 0 | return 0; |
68 | 0 | } |
69 | | |
70 | | // Write first half of fuzz data to a table file. |
71 | 95 | char new_file[256]; |
72 | 95 | sprintf(new_file, "/tmp/libfuzzer-%d.ctb", counter); |
73 | 95 | counter++; |
74 | | |
75 | 95 | FILE *fp = fopen(new_file, "wb"); |
76 | 95 | if (!fp) { |
77 | 0 | return 0; |
78 | 0 | } |
79 | 95 | fwrite(data, 512, 1, fp); |
80 | 95 | fclose(fp); |
81 | | |
82 | | // Adjust data pointer to after the table file data. |
83 | 95 | data += 512; |
84 | 95 | size -= 512; |
85 | | |
86 | | /* check if this works, otherwise bail */ |
87 | 95 | if (lou_checkTable(new_file) == 0) { |
88 | 16 | lou_free(); |
89 | 16 | unlink(new_file); |
90 | 16 | return 0; |
91 | 16 | } |
92 | 79 | char *mutable_data = NULL; |
93 | 79 | table_default = new_file; |
94 | | |
95 | 79 | mutable_data = strndup((char*)data, size); |
96 | 79 | if (!mutable_data) |
97 | 0 | { |
98 | 0 | perror("malloc"); |
99 | 0 | exit(1); |
100 | 0 | } |
101 | | |
102 | 79 | widechar *inputText = malloc((size*16+1)*sizeof(widechar)); |
103 | 79 | int len = (int)_lou_extParseChars(mutable_data, inputText); |
104 | 79 | free(mutable_data); |
105 | 79 | if (len <= 0) { |
106 | 7 | free(inputText); |
107 | 7 | unlink(new_file); |
108 | 7 | lou_free(); |
109 | 7 | return -1; |
110 | 7 | } |
111 | | |
112 | 72 | assert(len <= (size*16)); |
113 | 72 | inputLen = len; |
114 | 72 | outputLen = len*16; |
115 | 72 | widechar *outputText = malloc((outputLen+1)*sizeof(widechar)); |
116 | | |
117 | 72 | lou_translateString(table_default, inputText, &inputLen, outputText, &outputLen, NULL, NULL, ucBrl); |
118 | | |
119 | 72 | free(inputText); |
120 | 72 | free(outputText); |
121 | 72 | lou_free(); |
122 | 72 | unlink(new_file); |
123 | | |
124 | 72 | return 0; |
125 | 72 | } |