From 3888b1642a35990ecc70b46b30d38a2b4bc61bd9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 31 Jul 2020 18:59:22 +0200 Subject: [PATCH] :bug: fix lexer to properly cope with repeated comments #2330 --- include/nlohmann/detail/input/lexer.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-class_lexer.cpp | 3 +++ test/src/unit-regression2.cpp | 7 +++++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 60eb3526f0..b255b9d200 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -1511,7 +1511,7 @@ class lexer : public lexer_base skip_whitespace(); // ignore comments - if (ignore_comments && current == '/') + while (ignore_comments && current == '/') { if (!scan_comment()) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9c66a8457f..723773ded7 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -7390,7 +7390,7 @@ class lexer : public lexer_base skip_whitespace(); // ignore comments - if (ignore_comments && current == '/') + while (ignore_comments && current == '/') { if (!scan_comment()) { diff --git a/test/src/unit-class_lexer.cpp b/test/src/unit-class_lexer.cpp index 0f544d5553..6d4ede8a5e 100644 --- a/test/src/unit-class_lexer.cpp +++ b/test/src/unit-class_lexer.cpp @@ -241,5 +241,8 @@ TEST_CASE("lexer class") CHECK((scan_string("/* true */", true) == json::lexer::token_type::end_of_input)); CHECK((scan_string("/*/**/", true) == json::lexer::token_type::end_of_input)); CHECK((scan_string("/*/* */", true) == json::lexer::token_type::end_of_input)); + + CHECK((scan_string("//\n//\n", true) == json::lexer::token_type::end_of_input)); + CHECK((scan_string("/**//**//**/", true) == json::lexer::token_type::end_of_input)); } } diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index d445f068e8..1f8c527de7 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -478,4 +478,11 @@ TEST_CASE("regression tests 2") CHECK(jsonObj["aaaa"] == 11); CHECK(jsonObj["bbb"] == 222); } + + SECTION("issue #2330 - ignore_comment=true fails on multiple consecutive lines starting with comments") + { + std::string ss = "//\n//\n{\n}\n"; + json j = json::parse(ss, nullptr, true, true); + CHECK(j.dump() == "{}"); + } }