diff --git a/Makefile b/Makefile index 1b7cf8c1..f52511c6 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,7 @@ doctest: # -Wno-documentation-unknown-command: code uses user-defined commands like @complexity # -Wno-exit-time-destructors: warning in Catch code # -Wno-keyword-macro: unit-tests use "#define private public" +# -Wno-weak-vtables: exception class is defined inline, but has virtual method # -Wno-range-loop-analysis: iterator_wrapper tests tests "for(const auto i...)" pedantic: $(MAKE) json_unit CXXFLAGS="\ @@ -58,6 +59,7 @@ pedantic: -Wno-documentation-unknown-command \ -Wno-exit-time-destructors \ -Wno-keyword-macro \ + -Wno-weak-vtables \ -Wno-range-loop-analysis" diff --git a/src/json.hpp b/src/json.hpp index d7108ed6..6a36b85d 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -156,7 +156,8 @@ file. @note For an input with n bytes, 1 is the index of the first character and n+1 is the index of the terminating null byte or the end of - file. + file. This also holds true when reading a byte vector (CBOR or + MessagePack). Exceptions have ids 1xx. @@ -171,6 +172,8 @@ json.exception.[parse_error](@ref parse_error).106 | "parse error: array index ' json.exception.[parse_error](@ref parse_error).107 | "parse error: JSON pointer must be empty or begin with '/' - was: 'foo'" | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. json.exception.[parse_error](@ref parse_error).108 | "parse error: escape character '~' must be followed with '0' or '1'" | In a JSON Pointer, only `~0` and `~1` are valid escape sequences. json.exception.[parse_error](@ref parse_error).109 | "parse error: array index 'one' is not a number" | A JSON Pointer array index must be a number. +json.exception.[parse_error](@ref parse_error).110 | "parse error at 1: cannot read 2 bytes from vector" | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. + @since version 3.0.0 */ @@ -198,7 +201,8 @@ class parse_error : public exception @note For an input with n bytes, 1 is the index of the first character and n+1 is the index of the terminating null byte or the end of - file. + file. This also holds true when reading a byte vector (CBOR or + MessagePack). */ const size_t byte; }; @@ -7344,7 +7348,7 @@ class basic_json @tparam T the integral return type - @throw std::out_of_range if there are less than sizeof(T)+1 bytes in the + @throw parse_error.110 if there are less than sizeof(T)+1 bytes in the vector @a vec to read In the for loop, the bytes from the vector are copied in reverse order into @@ -7371,7 +7375,7 @@ class basic_json { if (current_index + sizeof(T) + 1 > vec.size()) { - JSON_THROW(std::out_of_range("cannot read " + std::to_string(sizeof(T)) + " bytes from vector")); + JSON_THROW(parse_error(110, current_index + 1, "cannot read " + std::to_string(sizeof(T)) + " bytes from vector")); } T result; diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 4466a509..4ee6cced 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -156,7 +156,8 @@ file. @note For an input with n bytes, 1 is the index of the first character and n+1 is the index of the terminating null byte or the end of - file. + file. This also holds true when reading a byte vector (CBOR or + MessagePack). Exceptions have ids 1xx. @@ -171,6 +172,8 @@ json.exception.[parse_error](@ref parse_error).106 | "parse error: array index ' json.exception.[parse_error](@ref parse_error).107 | "parse error: JSON pointer must be empty or begin with '/' - was: 'foo'" | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. json.exception.[parse_error](@ref parse_error).108 | "parse error: escape character '~' must be followed with '0' or '1'" | In a JSON Pointer, only `~0` and `~1` are valid escape sequences. json.exception.[parse_error](@ref parse_error).109 | "parse error: array index 'one' is not a number" | A JSON Pointer array index must be a number. +json.exception.[parse_error](@ref parse_error).110 | "parse error at 1: cannot read 2 bytes from vector" | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. + @since version 3.0.0 */ @@ -198,7 +201,8 @@ class parse_error : public exception @note For an input with n bytes, 1 is the index of the first character and n+1 is the index of the terminating null byte or the end of - file. + file. This also holds true when reading a byte vector (CBOR or + MessagePack). */ const size_t byte; }; @@ -7344,7 +7348,7 @@ class basic_json @tparam T the integral return type - @throw std::out_of_range if there are less than sizeof(T)+1 bytes in the + @throw parse_error.110 if there are less than sizeof(T)+1 bytes in the vector @a vec to read In the for loop, the bytes from the vector are copied in reverse order into @@ -7371,7 +7375,7 @@ class basic_json { if (current_index + sizeof(T) + 1 > vec.size()) { - JSON_THROW(std::out_of_range("cannot read " + std::to_string(sizeof(T)) + " bytes from vector")); + JSON_THROW(parse_error(110, current_index + 1, "cannot read " + std::to_string(sizeof(T)) + " bytes from vector")); } T result; diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 84b280bc..9525c722 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -1145,21 +1145,52 @@ TEST_CASE("CBOR") { SECTION("too short byte vector") { - CHECK_THROWS_AS(json::from_cbor(std::vector({0x18})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x19})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x19, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a, 0x00, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), std::out_of_range); - CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), std::out_of_range); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x18})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x19})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x19, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1a, 0x00, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error); + CHECK_THROWS_AS(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error); + + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x18})), + "[json.exception.parse_error.110] parse error at 1: cannot read 1 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x19})), + "[json.exception.parse_error.110] parse error at 1: cannot read 2 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x19, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 2 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1a})), + "[json.exception.parse_error.110] parse error at 1: cannot read 4 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1a, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 4 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1a, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 4 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1a, 0x00, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 4 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); + CHECK_THROWS_WITH(json::from_cbor(std::vector({0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), + "[json.exception.parse_error.110] parse error at 1: cannot read 8 bytes from vector"); } } }