Skip to content
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
10 changes: 8 additions & 2 deletions src/yajl/yajl_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ yajl_gen_free(yajl_gen g)
}

#define INCREMENT_DEPTH \
if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
do { \
if (g->depth >= YAJL_MAX_DEPTH - 1) return yajl_max_depth_exceeded; \
g->depth++; \
} while (0)

#define DECREMENT_DEPTH \
if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_gen_generation_complete;
do { \
if (g->depth == 0) return yajl_gen_generation_complete; \
g->depth--; \
} while (0)

#define APPENDED_ATOM \
switch (g->state[g->depth]) { \
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-prettify.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# test case from https://github.com/lloyd/yajl/issues/204
test_that("depth limit is enforced to avoid out-of-bounds", {
# Deep nesting limit checks
deep_array <- strrep("[", 130L)
expect_error(minify(deep_array), "client cancelled parse")
expect_error(prettify(deep_array), "client cancelled parse")

err_str <- "[[{} ,[[[{} ,[{} ,[[],[[[9],[9],[],[[[9],[[[[],[[9],[9],[[9],[[[[],[[9],[[[9],[[],[[[[],[[9 ,[{} ,[[],[[[9],[9],[],[[[9],[[[[[9],[[[[],[[9],[9],[7],[],[[[9],[[[[[9],[[[[],[[9],[[[9],[[],[[[[],[[9],[9],[],[[[9],[[[[[9],[[[[],[[],[[9],[[[[],[[9],[[],[],[]],[[]],[],[],[[9],[[],[],[[],[[],9],[],[[[9],[[[[[9],[],[9],[],[[[9],[[[[[9],[[[[],[[9],[[[[],[[9],[[],[],[]],[[],[[9],[[9],[9],[],[[[9],[[[[[9],[[[[],[[9],[[[[],[[9],[[],[],[]],[[]],[],[],[[9],[[],[],[[],[[],[[[9],[9],[],[[[9],[9],[[[[],[[9],[[[[],[[9],[[],[],[]],[[[9],[9],[[[[],[[9],[[[[],[[9],[[],[],[]],[[]]C[],[],[[9],[[],[],[[],[[],[[[9],[9],[],[[[9],[[],[[[[],[[9],[[[[["
expect_error(minify(err_str), "client cancelled parse")
expect_error(prettify(err_str), "client cancelled parse")
})

# test case from https://github.com/lloyd/yajl/issues/205
test_that("no invalid read on deep array with unclosed string", {
err_str <- '{"[9[":[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[4[[[[[[[[[[[[[[[[[[[[[[[[H[[[[[[[[[[K[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[E[[[[[[[[[[[[[[j[[[[[[[[[[[[[][[[[[[[[[[[[[[[[[[":[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[g[@[[[[[[[[[[[[[[[[\\M[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[k[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[\n'
expect_error(minify(err_str), "client cancelled parse")
expect_error(prettify(err_str), "client cancelled parse")
})

# test case from https://github.com/lloyd/yajl/issues/206
test_that("no uninitialised read on malformed deep array", {
err_str <- '[[[[[{"ARY":[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[],{"AR":[{"URY":[[[],\t"",{" R":[\t"",{" R":{" R":[\t"",{" R":[[],{"AR":{" ;":[[[[],{"AR":[{"URY":[[],[[[9],[],\t"",{" R":[\t"",{" R":["",{" R":[\t"",{" R":[[],{"AR R":[[],{" URY":[[[9],[:[\x16"",{" R":["",{" R":[\t"",{" R"D[[],{"AR":[[],{"AR":[{"URAR":{" R":[[],\t"",[],{"AR":[ ,[{"Y":[[],{"AR":[{"URY":[[],[\t"",{" R":[\t"",{" [[[[[],{" R":[\t"",{" R" [[],{"RY":[[],{"AR":[{"URY":[[],[[[9],[],\t"",{" R":[\t"",{" R":["",{" R":[\t"",{" R":[[],{"AR":{" R":[[],{" URY":[[],[[[9],[],{" R":[\t""{"[\t\n'
expect_error(minify(err_str), "client cancelled parse")
expect_error(prettify(err_str), "client cancelled parse")
})
Loading