Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend fuzzing set up #2952

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/jv.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ enum {

jv jv_parse(const char* string);
jv jv_parse_sized(const char* string, int length);
jv jv_parse_custom_flags(const char* string, int flags);

typedef void (*jv_nomem_handler_f)(void *);
void jv_nomem_handler(jv_nomem_handler_f, void *);
Expand Down
12 changes: 10 additions & 2 deletions src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,9 @@ jv jv_parser_next(struct jv_parser* p) {
}
}

jv jv_parse_sized(const char* string, int length) {
jv jv_parse_sized_custom_flags(const char* string, int length, int flags) {
struct jv_parser parser;
parser_init(&parser, 0);
parser_init(&parser, flags);
jv_parser_set_buf(&parser, string, length, 0);
jv value = jv_parser_next(&parser);
if (jv_is_valid(value)) {
Expand Down Expand Up @@ -898,6 +898,14 @@ jv jv_parse_sized(const char* string, int length) {
return value;
}

jv jv_parse_sized(const char* string, int length) {
return jv_parse_sized_custom_flags(string, length, 0);
}

jv jv_parse(const char* string) {
return jv_parse_sized(string, strlen(string));
}

jv jv_parse_custom_flags(const char* string, int flags) {
return jv_parse_sized_custom_flags(string, strlen(string), flags);
}
35 changes: 35 additions & 0 deletions tests/jq_fuzz_parse_extended.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "jv.h"

int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
if (size < 8) {
return 0;
}

int fuzz_flags = *(int*)data;
data += 4;
size -= 4;
int dump_flags = *(int*)data;
data += 4;
size -= 4;

// Creat null-terminated string
char *null_terminated = (char *)malloc(size + 1);
memcpy(null_terminated, (char *)data, size);
null_terminated[size] = '\0';

// Fuzzer entrypoint
jv res = jv_parse_custom_flags(null_terminated, fuzz_flags);
if (jv_is_valid(res)) {
jv_dump(res, dump_flags);
}
jv_free(res);

// Free the null-terminated string
free(null_terminated);

return 0;
}
21 changes: 21 additions & 0 deletions tests/jq_fuzz_parse_stream.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "jv.h"

int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
// Creat null-terminated string
char *null_terminated = (char *)malloc(size + 1);
memcpy(null_terminated, (char *)data, size);
null_terminated[size] = '\0';

// Fuzzer entrypoint
jv res = jv_parse_custom_flags(null_terminated, JV_PARSE_STREAMING);
jv_free(res);

// Free the null-terminated string
free(null_terminated);

return 0;
}