/src/hunspell/src/tools/fuzzer.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* ***** BEGIN LICENSE BLOCK ***** |
3 | | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
4 | | * |
5 | | * The contents of this file are subject to the Mozilla Public License Version |
6 | | * 1.1 (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * http://www.mozilla.org/MPL/ |
9 | | * |
10 | | * Software distributed under the License is distributed on an "AS IS" basis, |
11 | | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
12 | | * for the specific language governing rights and limitations under the |
13 | | * License. |
14 | | * |
15 | | * Alternatively, the contents of this file may be used under the terms of |
16 | | * either the GNU General Public License Version 2 or later (the "GPL"), or |
17 | | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
18 | | * in which case the provisions of the GPL or the LGPL are applicable instead |
19 | | * of those above. If you wish to allow use of your version of this file only |
20 | | * under the terms of either the GPL or the LGPL, and not to allow others to |
21 | | * use your version of this file under the terms of the MPL, indicate your |
22 | | * decision by deleting the provisions above and replace them with the notice |
23 | | * and other provisions required by the GPL or the LGPL. If you do not delete |
24 | | * the provisions above, a recipient may use your version of this file under |
25 | | * the terms of any one of the MPL, the GPL or the LGPL. |
26 | | * |
27 | | * ***** END LICENSE BLOCK ***** */ |
28 | | |
29 | | #include <hunspell/hunspell.hxx> |
30 | | #include <sys/types.h> |
31 | | #include <dirent.h> |
32 | | #include <string.h> |
33 | | #include <libgen.h> |
34 | | #include <memory> |
35 | | |
36 | | std::vector<std::unique_ptr<Hunspell>> dictionaries; |
37 | | |
38 | | bool endswith(const std::string &str, const std::string &suffix) |
39 | 0 | { |
40 | 0 | return str.size() >= suffix.size() && |
41 | 0 | str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; |
42 | 0 | } |
43 | | |
44 | | extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
45 | 0 | { |
46 | 0 | char* exe_path = (*argv)[0]; |
47 | | // dirname() can modify its argument. |
48 | 0 | char* exe_path_copy = strdup(exe_path); |
49 | 0 | char* dir = dirname(exe_path_copy); |
50 | 0 | DIR* d = opendir(dir); |
51 | 0 | struct dirent *direntry; |
52 | 0 | while ((direntry = readdir(d)) != NULL) |
53 | 0 | { |
54 | 0 | std::string entry(direntry->d_name); |
55 | 0 | if (endswith(entry, ".aff")) |
56 | 0 | { |
57 | 0 | std::string dic = entry.substr(0, entry.size() - 4) + ".dic"; |
58 | 0 | dictionaries.emplace_back(new Hunspell(entry.c_str(), dic.c_str())); |
59 | 0 | } |
60 | 0 | } |
61 | 0 | closedir(d); |
62 | 0 | free(exe_path_copy); |
63 | |
|
64 | 0 | return 0; |
65 | 0 | } |
66 | | |
67 | | extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) |
68 | | { |
69 | | std::string word(data, size); |
70 | | for (auto& dict : dictionaries) |
71 | | { |
72 | | if (!dict->spell(word)) |
73 | | dict->suggest(word); |
74 | | } |
75 | | return 0; |
76 | | } |
77 | | |
78 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |