From ccf35754f1a5d56e2a8f0d0c13c5be656b6bce6d Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Sat, 7 Nov 2015 13:34:47 -0500 Subject: [PATCH] parser: Ensure multiple values cannot follow each other Fixes #15 --- Makefile.am | 1 + lib/univalue_read.cpp | 13 ++++++++++++- test/fail37.json | 1 + test/unitester.cpp | 3 ++- 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/fail37.json diff --git a/Makefile.am b/Makefile.am index 20cf523ed8bfc..3e9dc0fbbd98d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,6 +72,7 @@ TEST_FILES = \ $(TEST_DATA_DIR)/fail34.json \ $(TEST_DATA_DIR)/fail35.json \ $(TEST_DATA_DIR)/fail36.json \ + $(TEST_DATA_DIR)/fail37.json \ $(TEST_DATA_DIR)/fail3.json \ $(TEST_DATA_DIR)/fail4.json \ $(TEST_DATA_DIR)/fail5.json \ diff --git a/lib/univalue_read.cpp b/lib/univalue_read.cpp index 6b956c3f9323a..c7516b9628169 100644 --- a/lib/univalue_read.cpp +++ b/lib/univalue_read.cpp @@ -244,8 +244,9 @@ enum jtokentype getJsonToken(string& tokenVal, unsigned int& consumed, enum expect_bits { EXP_OBJ_NAME = (1U << 0), EXP_COLON = (1U << 1), - EXP_VALUE = (1U << 3), EXP_ARR_VALUE = (1U << 2), + EXP_VALUE = (1U << 3), + EXP_NOT_VALUE = (1U << 4), }; #define expect(bit) (expectMask & (EXP_##bit)) @@ -300,6 +301,12 @@ bool UniValue::read(const char *raw) return false; } + if (expect(NOT_VALUE)) { + if (isValueOpen) + return false; + clearExpect(NOT_VALUE); + } + switch (tok) { case JTOK_OBJ_OPEN: @@ -339,6 +346,7 @@ bool UniValue::read(const char *raw) stack.pop_back(); clearExpect(OBJ_NAME); + setExpect(NOT_VALUE); break; } @@ -390,6 +398,7 @@ bool UniValue::read(const char *raw) UniValue *top = stack.back(); top->values.push_back(tmpVal); + setExpect(NOT_VALUE); break; } @@ -401,6 +410,7 @@ bool UniValue::read(const char *raw) UniValue *top = stack.back(); top->values.push_back(tmpVal); + setExpect(NOT_VALUE); break; } @@ -419,6 +429,7 @@ bool UniValue::read(const char *raw) top->values.push_back(tmpVal); } + setExpect(NOT_VALUE); break; } diff --git a/test/fail37.json b/test/fail37.json new file mode 100644 index 0000000000000..3294dc3a42529 --- /dev/null +++ b/test/fail37.json @@ -0,0 +1 @@ +{"a":1 "b":2} diff --git a/test/unitester.cpp b/test/unitester.cpp index 58331077da49a..fc2f078aee5fa 100644 --- a/test/unitester.cpp +++ b/test/unitester.cpp @@ -93,8 +93,9 @@ static const char *filenames[] = { "fail32.json", "fail33.json", "fail34.json", - // "fail35.json", // investigate - issue #15 + "fail35.json", "fail36.json", + "fail37.json", "fail3.json", "fail4.json", // extra comma "fail5.json",