diff --git a/include/nlohmann/detail/view/builder.hpp b/include/nlohmann/detail/view/builder.hpp index 1bcbbb4c0..ef8dcb160 100644 --- a/include/nlohmann/detail/view/builder.hpp +++ b/include/nlohmann/detail/view/builder.hpp @@ -262,10 +262,8 @@ class builder } if (c != '\\') { - if (NulIsEnd && c == 0) - { - return failed(error_code::string_missing_quote, p); - } + // (a NUL before the end of the input is a control character, as + // for json::parse, also where a NUL ends the input between values) return failed(c < 0x20 ? error_code::string_control_character : error_code::string_utf8, p); } ++p; diff --git a/tests/src/unit-json_view_builder.cpp b/tests/src/unit-json_view_builder.cpp index ad44e9bc3..065bf0cee 100644 --- a/tests/src/unit-json_view_builder.cpp +++ b/tests/src/unit-json_view_builder.cpp @@ -255,6 +255,18 @@ TEST_CASE("json_view builder") SECTION("NUL, BOM, and whitespace") { + // a NUL inside a string is a control character, as for json::parse + // (where a NUL ends the input, it does so only between values) + for (const bool sentinel : + { + true, false + }) + { + const built b = build(std::string("[\"ab\0cd\"]", 9), false, false, sentinel); + CHECK(!b.ok); + CHECK(b.failure.code == nlohmann::detail::view::error_code::string_control_character); + CHECK(b.failure.offset == 4); + } check_same(std::string("[1]\0garbage", 11)); check_same(std::string("[1\0]", 4)); check_same(std::string("[1, // c\0\n2]", 12));