Skip to content

Commit 2f5591a

Browse files
test: add fuzzing set up
PR-URL: #114 Reviewed-By: Fedor Indutny <[email protected]>
1 parent 0043973 commit 2f5591a

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

test/fuzzers/fuzz_parser.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "llhttp.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
int handle_on_message_complete(llhttp_t *arg) { return 0; }
7+
8+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
9+
llhttp_t parser;
10+
llhttp_settings_t settings;
11+
llhttp_type_t http_type;
12+
13+
/* We need four bytes to determine variable parameters */
14+
if (size < 4) {
15+
return 0;
16+
}
17+
18+
int headers = (data[0] & 0x01) == 1;
19+
int chunked_length = (data[1] & 0x01) == 1;
20+
int keep_alive = (data[2] & 0x01) == 1;
21+
if (data[0] % 3 == 0) {
22+
http_type = HTTP_BOTH;
23+
} else if (data[0] % 3 == 1) {
24+
http_type = HTTP_REQUEST;
25+
} else {
26+
http_type = HTTP_RESPONSE;
27+
}
28+
data += 4;
29+
size -= 4;
30+
31+
/* Initialize user callbacks and settings */
32+
llhttp_settings_init(&settings);
33+
34+
/* Set user callback */
35+
settings.on_message_complete = handle_on_message_complete;
36+
37+
llhttp_init(&parser, http_type, &settings);
38+
llhttp_set_lenient_headers(&parser, headers);
39+
llhttp_set_lenient_chunked_length(&parser, chunked_length);
40+
llhttp_set_lenient_keep_alive(&parser, keep_alive);
41+
42+
llhttp_execute(&parser, data, size);
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)