Skip to content

Instantly share code, notes, and snippets.

@skeeto
Last active November 8, 2022 00:54
Show Gist options
  • Save skeeto/93891c57a83b2562389959926a9cb364 to your computer and use it in GitHub Desktop.
Save skeeto/93891c57a83b2562389959926a9cb364 to your computer and use it in GitHub Desktop.
Jsonic fuzz tester
// Fuzz test for Jsonic
// $ afl-gcc -m32 -fsanitize=address,undefined fuzz.c jsonic.c
// $ afl-fuzz -m800 -iexamples/heroes -oout ./a.out
// https://github.com/rohanrhu/jsonic
// This is free and unencumbered software released into the public domain.
#include <stdio.h>
#include <stdlib.h>
#include "jsonic.h"
static int explore(jsonic_node_t *root, char *buf)
{
jsonic_node_t *key = 0;
switch (root->type) {
case JSONIC_NONE:
return 1;
case JSONIC_OBJECT:
for (;;) {
key = jsonic_object_iter_kv_free(buf, root, key);
if (key->type == JSONIC_NONE) {
return 0;
}
printf("=> %s\n", key->key);
if (explore(key, buf)) {
return 1;
}
}
case JSONIC_ARRAY:
for (;;) {
jsonic_node_t *e = jsonic_array_iter_free(buf, root, e, 0);
if (e->type == JSONIC_NONE) {
return 0;
}
if (explore(e, buf)) {
return 1;
}
}
case JSONIC_STRING:
case JSONIC_NUMBER:
case JSONIC_BOOLEAN:
case JSONIC_NULL:
puts(root->val);
return 0;
}
abort();
}
int main(void)
{
char *buf = malloc(1<<10);
int len = fread(buf, 1, (1<<10)-1, stdin);
buf[len++] = 0;
buf = realloc(buf, len);
return explore(jsonic_get_root(buf), buf);
}
@rohanrhu
Copy link

rohanrhu commented Nov 8, 2022

It is nice just ~70 SLOC 🙀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment