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

Add and fix JSON stack overflow tests #74

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions ccan/json/_info
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
* "likes_pizza": true
* }
* ]
*
* The JSON standard supports null characters in strings, due to
* difficulties in handling nulls in C strings this is not supported by
* this library, json_decode() will return NULL.
*
* There is also a depth limit of 100 to prevent runaway recursion.
*
* Example:
* #include <ccan/json/json.h>
Expand Down
12 changes: 12 additions & 0 deletions ccan/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <stdlib.h>
#include <string.h>

#define MAX_DEPTH 100

#define out_of_memory() do { \
fprintf(stderr, "Out of memory.\n"); \
exit(EXIT_FAILURE); \
Expand Down Expand Up @@ -623,6 +625,10 @@ void json_remove_from_parent(JsonNode *node)
static bool parse_value(const char **sp, JsonNode **out)
{
const char *s = *sp;
static int depth = 0;

if (depth > MAX_DEPTH)
return false; // Prevent stack overflow

switch (*s) {
case 'n':
Expand Down Expand Up @@ -664,17 +670,23 @@ static bool parse_value(const char **sp, JsonNode **out)
}

case '[':
depth++;
if (parse_array(&s, out)) {
*sp = s;
depth--;
return true;
}
depth--;
return false;

case '{':
depth++;
if (parse_object(&s, out)) {
*sp = s;
depth--;
return true;
}
depth--;
return false;

default: {
Expand Down
5 changes: 4 additions & 1 deletion ccan/json/test/run-decode-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main(void)
const char *strings_file = "test/test-strings";
const char *strings_reencoded_file = "test/test-strings-reencoded";
FILE *f, *f2;
char buffer[1024], buffer2[1024];
char buffer[250010], buffer2[1024];

plan_tests(90);

Expand All @@ -24,6 +24,9 @@ int main(void)
const char *s = chomp(buffer);
bool valid;
JsonNode *node;

diag("HELLO");
fprintf(stderr, "#%d %d %s\n", sizeof(buffer), strlen(buffer), buffer);

if (expect_literal(&s, "valid ")) {
valid = true;
Expand Down
6 changes: 3 additions & 3 deletions ccan/json/test/run-validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ int main(void)
{
const char *strings_file = "test/test-strings";
FILE *f;
char buffer[1024];
plan_tests(224);
char buffer[250010];

plan_tests(226);

f = fopen(strings_file, "rb");
if (f == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions ccan/json/test/test-strings

Large diffs are not rendered by default.