From 2d1d176a5599fedc4d424cd5a784b002f24ff04d Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Fri, 27 Nov 2015 16:31:43 +0100 Subject: [PATCH 01/16] add routine to detect c-style comments --- json11.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/json11.cpp b/json11.cpp index 0aa125b..e2085a1 100644 --- a/json11.cpp +++ b/json11.cpp @@ -364,6 +364,33 @@ struct JsonParser { i++; } + /* consume_comment() + * + * Advance comments (c-style inline and multiline). + */ + void consume_comment() { + if (str[i] == '/') { + i++; + if (str[i] == '/') { // inline comment + i++; + // advance until next line + while (str[i] != '\n') + i++; + consume_whitespace(); + consume_comment(); + } + else if (str[i] == '*') { // multiline comment + i++; + // advance until closing tokens + while (!(str[i] == '*' && str[i+1] == '/')) + i++; + i += 2; + consume_whitespace(); + consume_comment(); + } + } + } + /* get_next_token() * * Return the next non-whitespace character. If the end of the input is reached, From 08c391f89ad253eafb21fcad6b430a8c4df70c15 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Fri, 27 Nov 2015 16:41:05 +0100 Subject: [PATCH 02/16] introduce consume_garbage() new routine that consumes whitespaces and comments. activated by JSON11_COMMENTS pre-processor flag. --- json11.cpp | 18 +++++++++++++++--- json11.hpp | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/json11.cpp b/json11.cpp index e2085a1..4c473c1 100644 --- a/json11.cpp +++ b/json11.cpp @@ -391,13 +391,25 @@ struct JsonParser { } } + /* consume_garbage() + * + * Advance until the current character is non-whitespace and non-comment. + */ + void consume_garbage() { + consume_whitespace(); +#ifdef JSON11_COMMENTS + consume_comment(); + consume_whitespace(); +#endif + } + /* get_next_token() * * Return the next non-whitespace character. If the end of the input is reached, * flag an error and return 0. */ char get_next_token() { - consume_whitespace(); + consume_garbage(); if (i == str.size()) return fail("unexpected end of input", 0); @@ -689,7 +701,7 @@ Json Json::parse(const string &in, string &err) { Json result = parser.parse_json(0); // Check for any trailing garbage - parser.consume_whitespace(); + parser.consume_garbage(); if (parser.i != in.size()) return parser.fail("unexpected trailing " + esc(in[parser.i])); @@ -704,7 +716,7 @@ vector Json::parse_multi(const string &in, string &err) { while (parser.i != in.size() && !parser.failed) { json_vec.push_back(parser.parse_json(0)); // Check for another object - parser.consume_whitespace(); + parser.consume_garbage(); } return json_vec; } diff --git a/json11.hpp b/json11.hpp index fe9bba4..ff399d1 100644 --- a/json11.hpp +++ b/json11.hpp @@ -56,6 +56,8 @@ #include #include +#define JSON11_COMMENTS 1 + namespace json11 { class JsonValue; From de098c4d52e466049f85c2a378ef04b92cd3433a Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Fri, 27 Nov 2015 16:46:18 +0100 Subject: [PATCH 03/16] add testing for comment functionality --- test.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test.cpp b/test.cpp index 8b62aeb..7a40857 100644 --- a/test.cpp +++ b/test.cpp @@ -58,6 +58,28 @@ int main(int argc, char **argv) { std::cout << " - " << k.dump() << "\n"; } +#ifdef JSON11_COMMENTS + const string comment_test = R"({ + // comment + "a": 1, + // comment + // continued + "b": "text", + /* multi + line + comment */ + "c": [1, 2, 3] + })"; + + string err_comment; + auto json_comment = Json::parse(comment_test, err_comment); + if (!err_comment.empty()) { + printf("Failed: %s\n", err_comment.c_str()); + } else { + printf("Result: %s\n", json_comment.dump().c_str()); + } +#endif + std::list l1 { 1, 2, 3 }; std::vector l2 { 1, 2, 3 }; std::set l3 { 1, 2, 3 }; From 882feb56ac7e46c703be5a063775ba9da3decbab Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Mon, 30 Nov 2015 12:27:35 +0100 Subject: [PATCH 04/16] add bool to detect comments as run-time option. --- json11.cpp | 19 +++++++++++-------- json11.hpp | 16 ++++++++++------ test.cpp | 5 ++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/json11.cpp b/json11.cpp index 4c473c1..4d239c0 100644 --- a/json11.cpp +++ b/json11.cpp @@ -338,6 +338,7 @@ struct JsonParser { size_t i; string &err; bool failed; + bool detect_comments; /* fail(msg, err_ret = Json()) * @@ -397,10 +398,10 @@ struct JsonParser { */ void consume_garbage() { consume_whitespace(); -#ifdef JSON11_COMMENTS - consume_comment(); - consume_whitespace(); -#endif + if(detect_comments) { + consume_comment(); + consume_whitespace(); + } } /* get_next_token() @@ -696,8 +697,8 @@ struct JsonParser { } }; -Json Json::parse(const string &in, string &err) { - JsonParser parser { in, 0, err, false }; +Json Json::parse(const string &in, string &err, bool detect_comments) { + JsonParser parser { in, 0, err, false, detect_comments }; Json result = parser.parse_json(0); // Check for any trailing garbage @@ -709,8 +710,10 @@ Json Json::parse(const string &in, string &err) { } // Documented in json11.hpp -vector Json::parse_multi(const string &in, string &err) { - JsonParser parser { in, 0, err, false }; +vector Json::parse_multi(const string &in, + string &err, + bool detect_comments) { + JsonParser parser { in, 0, err, false, detect_comments }; vector json_vec; while (parser.i != in.size() && !parser.failed) { diff --git a/json11.hpp b/json11.hpp index ff399d1..582c43f 100644 --- a/json11.hpp +++ b/json11.hpp @@ -56,8 +56,6 @@ #include #include -#define JSON11_COMMENTS 1 - namespace json11 { class JsonValue; @@ -147,17 +145,23 @@ public: } // Parse. If parse fails, return Json() and assign an error message to err. - static Json parse(const std::string & in, std::string & err); - static Json parse(const char * in, std::string & err) { + static Json parse(const std::string & in, + std::string & err, + bool detect_comments = false); + static Json parse(const char * in, + std::string & err, + bool detect_comments = false) { if (in) { - return parse(std::string(in), err); + return parse(std::string(in), err, detect_comments); } else { err = "null input"; return nullptr; } } // Parse multiple objects, concatenated or separated by whitespace - static std::vector parse_multi(const std::string & in, std::string & err); + static std::vector parse_multi(const std::string & in, + std::string & err, + bool detect_comments = false); bool operator== (const Json &rhs) const; bool operator< (const Json &rhs) const; diff --git a/test.cpp b/test.cpp index 7a40857..26fd5b8 100644 --- a/test.cpp +++ b/test.cpp @@ -58,7 +58,6 @@ int main(int argc, char **argv) { std::cout << " - " << k.dump() << "\n"; } -#ifdef JSON11_COMMENTS const string comment_test = R"({ // comment "a": 1, @@ -72,13 +71,13 @@ int main(int argc, char **argv) { })"; string err_comment; - auto json_comment = Json::parse(comment_test, err_comment); + auto json_comment = Json::parse( + comment_test, err_comment, /*detect_comments=*/ true); if (!err_comment.empty()) { printf("Failed: %s\n", err_comment.c_str()); } else { printf("Result: %s\n", json_comment.dump().c_str()); } -#endif std::list l1 { 1, 2, 3 }; std::vector l2 { 1, 2, 3 }; From b05e655c0a37098ca8eaa106cfc78cd424bc4c55 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Mon, 30 Nov 2015 12:28:45 +0100 Subject: [PATCH 05/16] detect multiple comments with a loop instead of using recursion --- json11.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/json11.cpp b/json11.cpp index 4d239c0..a1efd61 100644 --- a/json11.cpp +++ b/json11.cpp @@ -369,7 +369,8 @@ struct JsonParser { * * Advance comments (c-style inline and multiline). */ - void consume_comment() { + bool consume_comment() { + bool comment_found = false; if (str[i] == '/') { i++; if (str[i] == '/') { // inline comment @@ -377,8 +378,7 @@ struct JsonParser { // advance until next line while (str[i] != '\n') i++; - consume_whitespace(); - consume_comment(); + comment_found = true; } else if (str[i] == '*') { // multiline comment i++; @@ -386,10 +386,10 @@ struct JsonParser { while (!(str[i] == '*' && str[i+1] == '/')) i++; i += 2; - consume_whitespace(); - consume_comment(); + comment_found = true; } } + return comment_found; } /* consume_garbage() @@ -399,8 +399,12 @@ struct JsonParser { void consume_garbage() { consume_whitespace(); if(detect_comments) { - consume_comment(); - consume_whitespace(); + bool comment_found = false; + do { + comment_found = consume_comment(); + consume_whitespace(); + } + while(comment_found); } } From 2f5c64225d4bbb9c5189d940cce65189c8d3c3ad Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Mon, 30 Nov 2015 12:40:20 +0100 Subject: [PATCH 06/16] detect malformed comments --- json11.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/json11.cpp b/json11.cpp index a1efd61..f67fda5 100644 --- a/json11.cpp +++ b/json11.cpp @@ -383,11 +383,16 @@ struct JsonParser { else if (str[i] == '*') { // multiline comment i++; // advance until closing tokens - while (!(str[i] == '*' && str[i+1] == '/')) - i++; + while (!(str[i] == '*' && str[i+1] == '/')) { + if (i == str.size()) + return fail( + "unexpected end of input inside multi-line comment", 0); + i++;} i += 2; comment_found = true; } + else + return fail("malformed comment", 0); } return comment_found; } From d292fce9f23f394c381792581dcb22c8adabaef7 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Mon, 30 Nov 2015 12:42:24 +0100 Subject: [PATCH 07/16] improve comment test. test also for nested and mixed comments. whitespaces/newlines are already intermixed between comments. --- test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test.cpp b/test.cpp index 26fd5b8..87f27ed 100644 --- a/test.cpp +++ b/test.cpp @@ -59,7 +59,7 @@ int main(int argc, char **argv) { } const string comment_test = R"({ - // comment + // comment /* with nested comment */ "a": 1, // comment // continued @@ -67,6 +67,7 @@ int main(int argc, char **argv) { /* multi line comment */ + // and single-line comment "c": [1, 2, 3] })"; From f21b8c360e66527911258828a86c280c842202f4 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Mon, 30 Nov 2015 12:43:40 +0100 Subject: [PATCH 08/16] add malformed comment tests. add 3 testes for: - unended multi-line comment, - malformed single-line comment, - trailing slash --- test.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test.cpp b/test.cpp index 87f27ed..ba786d8 100644 --- a/test.cpp +++ b/test.cpp @@ -80,6 +80,45 @@ int main(int argc, char **argv) { printf("Result: %s\n", json_comment.dump().c_str()); } + string failing_comment_test = R"({ + /* bad comment + "a": 1, + })"; + + string err_failing_comment; + auto json_failing_comment = Json::parse( + failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + if (!err_failing_comment.empty()) { + printf("Failed: %s\n", err_failing_comment.c_str()); + } else { + printf("Result: %s\n", json_failing_comment.dump().c_str()); + } + + failing_comment_test = R"({ + / / bad comment + "a": 1, + })"; + + json_failing_comment = Json::parse( + failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + if (!err_failing_comment.empty()) { + printf("Failed: %s\n", err_failing_comment.c_str()); + } else { + printf("Result: %s\n", json_failing_comment.dump().c_str()); + } + + failing_comment_test = R"({ + "a": 1, + }/)"; + + json_failing_comment = Json::parse( + failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + if (!err_failing_comment.empty()) { + printf("Failed: %s\n", err_failing_comment.c_str()); + } else { + printf("Result: %s\n", json_failing_comment.dump().c_str()); + } + std::list l1 { 1, 2, 3 }; std::vector l2 { 1, 2, 3 }; std::set l3 { 1, 2, 3 }; From 4b0f5cfd774fd431f15858b15bd1152003f4d524 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 10:59:22 +0100 Subject: [PATCH 09/16] check for end of input on every increment of the cursor --- json11.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/json11.cpp b/json11.cpp index f67fda5..3ea9a67 100644 --- a/json11.cpp +++ b/json11.cpp @@ -373,22 +373,35 @@ struct JsonParser { bool comment_found = false; if (str[i] == '/') { i++; + if (i == str.size()) + return fail("unexpected end of input inside comment", 0); if (str[i] == '/') { // inline comment i++; + if (i == str.size()) + return fail("unexpected end of input inside inline comment", 0); // advance until next line - while (str[i] != '\n') + while (str[i] != '\n') { i++; + if (i == str.size()) + return fail("unexpected end of input inside inline comment", 0); + } comment_found = true; } else if (str[i] == '*') { // multiline comment i++; + if (i == str.size()) + return fail("unexpected end of input inside multi-line comment", 0); // advance until closing tokens while (!(str[i] == '*' && str[i+1] == '/')) { + i++; if (i == str.size()) return fail( "unexpected end of input inside multi-line comment", 0); - i++;} + } i += 2; + if (i == str.size()) + return fail( + "unexpected end of input inside multi-line comment", 0); comment_found = true; } else From 982b2d8885741bbef666f07ab874bbe802bb9d0b Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 11:00:11 +0100 Subject: [PATCH 10/16] improve testing for bad inline comments --- test.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test.cpp b/test.cpp index ba786d8..7d77aa3 100644 --- a/test.cpp +++ b/test.cpp @@ -95,9 +95,7 @@ int main(int argc, char **argv) { } failing_comment_test = R"({ - / / bad comment - "a": 1, - })"; + / / bad comment })"; json_failing_comment = Json::parse( failing_comment_test, err_failing_comment, /*detect_comments=*/ true); From aa270ad5b7fd62d8ae765d6ff58ca7ad527a00ea Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 11:00:58 +0100 Subject: [PATCH 11/16] fix test where the trailing / was not reached due to a previous error --- test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.cpp b/test.cpp index 7d77aa3..7e7d0e3 100644 --- a/test.cpp +++ b/test.cpp @@ -106,7 +106,7 @@ int main(int argc, char **argv) { } failing_comment_test = R"({ - "a": 1, + "a": 1 }/)"; json_failing_comment = Json::parse( From c6c6fcfeff5663cb34e7405bcc4572a2a5424d40 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 11:01:27 +0100 Subject: [PATCH 12/16] add test for inline comment without trailing newline --- test.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test.cpp b/test.cpp index 7e7d0e3..1e537b9 100644 --- a/test.cpp +++ b/test.cpp @@ -105,6 +105,16 @@ int main(int argc, char **argv) { printf("Result: %s\n", json_failing_comment.dump().c_str()); } + failing_comment_test = R"({// bad comment })"; + + json_failing_comment = Json::parse( + failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + if (!err_failing_comment.empty()) { + printf("Failed: %s\n", err_failing_comment.c_str()); + } else { + printf("Result: %s\n", json_failing_comment.dump().c_str()); + } + failing_comment_test = R"({ "a": 1 }/)"; From f9833b1e7d4b36901c36131c4699d889dae3eaf2 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 11:01:47 +0100 Subject: [PATCH 13/16] add test for unfinished multi-line comment --- test.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test.cpp b/test.cpp index 1e537b9..ece0e04 100644 --- a/test.cpp +++ b/test.cpp @@ -127,6 +127,17 @@ int main(int argc, char **argv) { printf("Result: %s\n", json_failing_comment.dump().c_str()); } + failing_comment_test = R"({/* bad + comment *})"; + + json_failing_comment = Json::parse( + failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + if (!err_failing_comment.empty()) { + printf("Failed: %s\n", err_failing_comment.c_str()); + } else { + printf("Result: %s\n", json_failing_comment.dump().c_str()); + } + std::list l1 { 1, 2, 3 }; std::vector l2 { 1, 2, 3 }; std::set l3 { 1, 2, 3 }; From 49a6197d08fbdd5a2eb9b0e8ae3aeab88f2a6d29 Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Tue, 1 Dec 2015 11:08:37 +0100 Subject: [PATCH 14/16] use an enum to select strategy on comment parsing --- json11.cpp | 12 ++++++------ json11.hpp | 17 +++++++++++------ test.cpp | 12 ++++++------ 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/json11.cpp b/json11.cpp index 3ea9a67..07e43c0 100644 --- a/json11.cpp +++ b/json11.cpp @@ -338,7 +338,7 @@ struct JsonParser { size_t i; string &err; bool failed; - bool detect_comments; + JsonParse strategy; /* fail(msg, err_ret = Json()) * @@ -416,7 +416,7 @@ struct JsonParser { */ void consume_garbage() { consume_whitespace(); - if(detect_comments) { + if(strategy == JsonParse::COMMENTS) { bool comment_found = false; do { comment_found = consume_comment(); @@ -719,8 +719,8 @@ struct JsonParser { } }; -Json Json::parse(const string &in, string &err, bool detect_comments) { - JsonParser parser { in, 0, err, false, detect_comments }; +Json Json::parse(const string &in, string &err, JsonParse strategy) { + JsonParser parser { in, 0, err, false, strategy }; Json result = parser.parse_json(0); // Check for any trailing garbage @@ -734,8 +734,8 @@ Json Json::parse(const string &in, string &err, bool detect_comments) { // Documented in json11.hpp vector Json::parse_multi(const string &in, string &err, - bool detect_comments) { - JsonParser parser { in, 0, err, false, detect_comments }; + JsonParse strategy) { + JsonParser parser { in, 0, err, false, strategy }; vector json_vec; while (parser.i != in.size() && !parser.failed) { diff --git a/json11.hpp b/json11.hpp index 582c43f..a99e241 100644 --- a/json11.hpp +++ b/json11.hpp @@ -58,6 +58,10 @@ namespace json11 { +enum JsonParse { + STANDARD, COMMENTS +}; + class JsonValue; class Json final { @@ -147,21 +151,22 @@ public: // Parse. If parse fails, return Json() and assign an error message to err. static Json parse(const std::string & in, std::string & err, - bool detect_comments = false); + JsonParse strategy = JsonParse::STANDARD); static Json parse(const char * in, std::string & err, - bool detect_comments = false) { + JsonParse strategy = JsonParse::STANDARD) { if (in) { - return parse(std::string(in), err, detect_comments); + return parse(std::string(in), err, strategy); } else { err = "null input"; return nullptr; } } // Parse multiple objects, concatenated or separated by whitespace - static std::vector parse_multi(const std::string & in, - std::string & err, - bool detect_comments = false); + static std::vector parse_multi( + const std::string & in, + std::string & err, + JsonParse strategy = JsonParse::STANDARD); bool operator== (const Json &rhs) const; bool operator< (const Json &rhs) const; diff --git a/test.cpp b/test.cpp index ece0e04..bd60705 100644 --- a/test.cpp +++ b/test.cpp @@ -73,7 +73,7 @@ int main(int argc, char **argv) { string err_comment; auto json_comment = Json::parse( - comment_test, err_comment, /*detect_comments=*/ true); + comment_test, err_comment, JsonParse::COMMENTS); if (!err_comment.empty()) { printf("Failed: %s\n", err_comment.c_str()); } else { @@ -87,7 +87,7 @@ int main(int argc, char **argv) { string err_failing_comment; auto json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -98,7 +98,7 @@ int main(int argc, char **argv) { / / bad comment })"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -108,7 +108,7 @@ int main(int argc, char **argv) { failing_comment_test = R"({// bad comment })"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -120,7 +120,7 @@ int main(int argc, char **argv) { }/)"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { @@ -131,7 +131,7 @@ int main(int argc, char **argv) { comment *})"; json_failing_comment = Json::parse( - failing_comment_test, err_failing_comment, /*detect_comments=*/ true); + failing_comment_test, err_failing_comment, JsonParse::COMMENTS); if (!err_failing_comment.empty()) { printf("Failed: %s\n", err_failing_comment.c_str()); } else { From 988a8fc249c49cfd0779eb3e938444b622eaad0a Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Wed, 2 Dec 2015 09:57:25 +0100 Subject: [PATCH 15/16] make JsonParser::strategy const --- json11.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json11.cpp b/json11.cpp index 07e43c0..3ec83af 100644 --- a/json11.cpp +++ b/json11.cpp @@ -338,7 +338,7 @@ struct JsonParser { size_t i; string &err; bool failed; - JsonParse strategy; + const JsonParse strategy; /* fail(msg, err_ret = Json()) * From ebc3a6b0387af83c6735f89dbde98ca425741b3b Mon Sep 17 00:00:00 2001 From: Antonio Cervone Date: Wed, 2 Dec 2015 10:01:29 +0100 Subject: [PATCH 16/16] watch out for i+1 to overflow the buffer --- json11.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/json11.cpp b/json11.cpp index 3ec83af..ebd7e93 100644 --- a/json11.cpp +++ b/json11.cpp @@ -389,12 +389,12 @@ struct JsonParser { } else if (str[i] == '*') { // multiline comment i++; - if (i == str.size()) + if (i > str.size()-2) return fail("unexpected end of input inside multi-line comment", 0); - // advance until closing tokens + // advance until closing tokens while (!(str[i] == '*' && str[i+1] == '/')) { i++; - if (i == str.size()) + if (i > str.size()-2) return fail( "unexpected end of input inside multi-line comment", 0); }