diff --git a/README.md b/README.md index 075206db..b2d7a155 100644 --- a/README.md +++ b/README.md @@ -969,6 +969,8 @@ I deeply appreciate the help of the following people. - [zerodefect](https://github.com/zerodefect) fixed a compiler warning. - [Kert](https://github.com/kaidokert) allowed to template the string type in the serialization and added the possibility to override the exceptional behavior. - [mark-99](https://github.com/mark-99) helped fixing an ICC error. +- [Patrik Huber](https://github.com/patrikhuber) fixed links in the README file. +- [johnfb](https://github.com/johnfb) found a bug in the implementation of CBOR's indefinite length strings. Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone. diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 057429cf..ca7bcaba 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -948,8 +948,7 @@ class binary_reader string_t result; while (get() != 0xFF) { - unexpect_eof(); - result.push_back(static_cast(current)); + result.append(get_cbor_string()); } return result; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9b38efb4..ad4712bd 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5768,8 +5768,7 @@ class binary_reader string_t result; while (get() != 0xFF) { - unexpect_eof(); - result.push_back(static_cast(current)); + result.append(get_cbor_string()); } return result; } diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 62accfcf..1d4767cd 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1381,7 +1381,7 @@ TEST_CASE("single CBOR roundtrip") std::ifstream f_json(filename); json j1 = json::parse(f_json); - // parse MessagePack file + // parse CBOR file std::ifstream f_cbor(filename + ".cbor", std::ios::binary); std::vector packed((std::istreambuf_iterator(f_cbor)), std::istreambuf_iterator()); @@ -1921,7 +1921,7 @@ TEST_CASE("examples from RFC 7049 Appendix A") CHECK(json::parse("\"\\ud800\\udd51\"") == json::from_cbor(std::vector({0x64, 0xf0, 0x90, 0x85, 0x91}))); // indefinite length strings - CHECK(json::parse("\"streaming\"") == json::from_cbor(std::vector({0x7f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0xff}))); + CHECK(json::parse("\"streaming\"") == json::from_cbor(std::vector({0x7f, 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x67, 0xff}))); } SECTION("arrays") diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index c3428532..dd8840d5 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -1408,4 +1408,19 @@ TEST_CASE("regression tests") "path": "/a/b/c"}])"_json), "[json.exception.out_of_range.403] key 'a' not found"); } + + SECTION("issue #961 - incorrect parsing of indefinite length CBOR strings") + { + std::vector v_cbor = + { + 0x7F, + 0x64, + 'a', 'b', 'c', 'd', + 0x63, + '1', '2', '3', + 0xFF + }; + json j = json::from_cbor(v_cbor); + CHECK(j == "abcd123"); + } }