From 8b457ace2533f92af1de132ad12ec0e579306c47 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 6 Feb 2018 20:43:03 +0100 Subject: [PATCH] :bug: fixing CBOR's indefinity length strings #961 Beside the fix discussed in #961, we also had to re-adjust a test case. It seems that it was failing before, and I "fixed" it to work with the broken implementation... --- README.md | 2 ++ include/nlohmann/detail/input/binary_reader.hpp | 3 +-- single_include/nlohmann/json.hpp | 3 +-- test/src/unit-cbor.cpp | 4 ++-- test/src/unit-regression.cpp | 15 +++++++++++++++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 075206db6..b2d7a1550 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 057429cf7..ca7bcabaf 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 9b38efb4a..ad4712bdb 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 62accfcf6..1d4767cd4 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 c34285326..dd8840d54 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"); + } }