| 1 |
/* libFuzzer harness for hyphen. |
| 2 |
* |
| 3 |
* Input format: [word bytes][NUL][dictionary bytes]. |
| 4 |
* If no NUL is present, the whole buffer is treated as the dictionary |
| 5 |
* and the word is empty (parser-only path). |
| 6 |
* |
| 7 |
* The dictionary is loaded via hnj_hyphen_load_data() to avoid filesystem I/O. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stddef.h> |
| 11 |
#include <stdint.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "hyphen.h" |
| 17 |
|
| 18 |
#define MAX_INPUT 65536 |
| 19 |
#define MAX_WORD_SIZE 1024 |
| 20 |
|
| 21 |
static void run_hyphenate(HyphenDict *dict, const char *word, int word_size) { |
| 22 |
if (word_size <= 0) return; |
| 23 |
|
| 24 |
size_t hyphens_size = (size_t)word_size + 5; |
| 25 |
size_t hyphword_size = (size_t)word_size * 2 + 1; |
| 26 |
|
| 27 |
/* hnj_hyphen_hyphenate2 */ |
| 28 |
{ |
| 29 |
char *hyphens = (char *)malloc(hyphens_size); |
| 30 |
char *hyphword = (char *)malloc(hyphword_size); |
| 31 |
char **rep = NULL; |
| 32 |
int *pos = NULL; |
| 33 |
int *cut = NULL; |
| 34 |
|
| 35 |
if (hyphens && hyphword) { |
| 36 |
memset(hyphens, 0, hyphens_size); |
| 37 |
memset(hyphword, 0, hyphword_size); |
| 38 |
(void)hnj_hyphen_hyphenate2(dict, word, word_size, |
| 39 |
hyphens, hyphword, |
| 40 |
&rep, &pos, &cut); |
| 41 |
if (rep) { |
| 42 |
for (int i = 0; i < word_size; i++) |
| 43 |
if (rep[i]) free(rep[i]); |
| 44 |
free(rep); |
| 45 |
free(pos); |
| 46 |
free(cut); |
| 47 |
} |
| 48 |
} |
| 49 |
free(hyphens); |
| 50 |
free(hyphword); |
| 51 |
} |
| 52 |
|
| 53 |
/* hnj_hyphen_hyphenate3 with non-trivial hyphenmin parameters to |
| 54 |
exercise the lhmin/rhmin/clhmin/crhmin paths. */ |
| 55 |
{ |
| 56 |
char *hyphens = (char *)malloc(hyphens_size); |
| 57 |
char *hyphword = (char *)malloc(hyphword_size); |
| 58 |
char **rep = NULL; |
| 59 |
int *pos = NULL; |
| 60 |
int *cut = NULL; |
| 61 |
|
| 62 |
if (hyphens && hyphword) { |
| 63 |
memset(hyphens, 0, hyphens_size); |
| 64 |
memset(hyphword, 0, hyphword_size); |
| 65 |
(void)hnj_hyphen_hyphenate3(dict, word, word_size, |
| 66 |
hyphens, hyphword, |
| 67 |
&rep, &pos, &cut, |
| 68 |
2, 2, 2, 2); |
| 69 |
if (rep) { |
| 70 |
for (int i = 0; i < word_size; i++) |
| 71 |
if (rep[i]) free(rep[i]); |
| 72 |
free(rep); |
| 73 |
free(pos); |
| 74 |
free(cut); |
| 75 |
} |
| 76 |
} |
| 77 |
free(hyphens); |
| 78 |
free(hyphword); |
| 79 |
} |
| 80 |
|
| 81 |
/* legacy hnj_hyphen_hyphenate */ |
| 82 |
{ |
| 83 |
char *hyphens = (char *)malloc(hyphens_size); |
| 84 |
if (hyphens) { |
| 85 |
memset(hyphens, 0, hyphens_size); |
| 86 |
(void)hnj_hyphen_hyphenate(dict, word, word_size, hyphens); |
| 87 |
} |
| 88 |
free(hyphens); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 93 |
if (size == 0 || size > MAX_INPUT) return 0; |
| 94 |
|
| 95 |
const uint8_t *word_buf = NULL; |
| 96 |
int word_size = 0; |
| 97 |
const uint8_t *dict_buf = data; |
| 98 |
size_t dict_size = size; |
| 99 |
|
| 100 |
const uint8_t *nul = memchr(data, 0, size); |
| 101 |
if (nul) { |
| 102 |
size_t prefix = (size_t)(nul - data); |
| 103 |
if (prefix > MAX_WORD_SIZE) prefix = MAX_WORD_SIZE; |
| 104 |
word_buf = data; |
| 105 |
word_size = (int)prefix; |
| 106 |
dict_buf = nul + 1; |
| 107 |
dict_size = size - (size_t)(nul - data) - 1; |
| 108 |
} |
| 109 |
|
| 110 |
if (dict_size == 0) return 0; |
| 111 |
|
| 112 |
HyphenDict *dict = hnj_hyphen_load_data((const char *)dict_buf, dict_size); |
| 113 |
if (!dict) return 0; |
| 114 |
|
| 115 |
if (word_size > 0) { |
| 116 |
char *word = (char *)malloc((size_t)word_size + 1); |
| 117 |
if (word) { |
| 118 |
memcpy(word, word_buf, (size_t)word_size); |
| 119 |
word[word_size] = 0; |
| 120 |
run_hyphenate(dict, word, word_size); |
| 121 |
free(word); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
hnj_hyphen_free(dict); |
| 126 |
return 0; |
| 127 |
} |