diff --git a/src/yajl/yajl_gen.c b/src/yajl/yajl_gen.c index 7c06a32..1f0ab44 100644 --- a/src/yajl/yajl_gen.c +++ b/src/yajl/yajl_gen.c @@ -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]) { \ diff --git a/tests/testthat/test-prettify.R b/tests/testthat/test-prettify.R new file mode 100644 index 0000000..edee1aa --- /dev/null +++ b/tests/testthat/test-prettify.R @@ -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") +})