mirror of
https://github.com/nlohmann/json.git
synced 2026-08-10 03:03:17 +00:00
Compare commits
4
Commits
2e23687092
...
dd24e2dffd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd24e2dffd | ||
|
|
868506dcc0 | ||
|
|
58ce09dcfd | ||
|
|
8dacb98041 |
@@ -38,14 +38,14 @@ jobs:
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@7188fc363630916deb702c7fdcf4e481b751f97a # v4.37.1
|
||||
uses: github/codeql-action/init@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
||||
with:
|
||||
languages: c-cpp
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@7188fc363630916deb702c7fdcf4e481b751f97a # v4.37.1
|
||||
uses: github/codeql-action/autobuild@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@7188fc363630916deb702c7fdcf4e481b751f97a # v4.37.1
|
||||
uses: github/codeql-action/analyze@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
||||
|
||||
@@ -43,6 +43,6 @@ jobs:
|
||||
output: 'flawfinder_results.sarif'
|
||||
|
||||
- name: Upload analysis results to GitHub Security tab
|
||||
uses: github/codeql-action/upload-sarif@7188fc363630916deb702c7fdcf4e481b751f97a # v4.37.1
|
||||
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
||||
with:
|
||||
sarif_file: ${{github.workspace}}/flawfinder_results.sarif
|
||||
|
||||
@@ -46,7 +46,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: "Run analysis"
|
||||
uses: ossf/scorecard-action@4eaacf0543bb3f2c246792bd56e8cdeffafb205a # v2.4.3
|
||||
uses: ossf/scorecard-action@2d1146689b8cda280b9bc96326124645441f03bc # v2.4.4
|
||||
with:
|
||||
results_file: results.sarif
|
||||
results_format: sarif
|
||||
@@ -76,6 +76,6 @@ jobs:
|
||||
|
||||
# Upload the results to GitHub's code scanning dashboard.
|
||||
- name: "Upload to code-scanning"
|
||||
uses: github/codeql-action/upload-sarif@7188fc363630916deb702c7fdcf4e481b751f97a # v4.37.1
|
||||
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
||||
with:
|
||||
sarif_file: results.sarif
|
||||
|
||||
@@ -61,7 +61,7 @@ jobs:
|
||||
|
||||
# Upload SARIF file generated in previous step
|
||||
- name: Upload SARIF file
|
||||
uses: github/codeql-action/upload-sarif@7188fc363630916deb702c7fdcf4e481b751f97a # v4.37.1
|
||||
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
||||
with:
|
||||
sarif_file: semgrep.sarif
|
||||
if: always()
|
||||
|
||||
@@ -13,9 +13,8 @@ is compatible with both of the binary data formats that use binary subtyping, (t
|
||||
incompatible with each other, and it is up to the user to translate between them). The subtype is added to `BinaryType`
|
||||
via the helper type [byte_container_with_subtype](../byte_container_with_subtype/index.md).
|
||||
|
||||
[CBOR's RFC 7049](https://tools.ietf.org/html/rfc7049) describes this type as:
|
||||
> Major type 2: a byte string. The string's length in bytes is represented following the rules for positive integers
|
||||
> (major type 0).
|
||||
[CBOR's RFC 8949](https://www.rfc-editor.org/rfc/rfc8949.html#section-3.1) describes this type as:
|
||||
> Major type 2: A byte string. The number of bytes in the string is equal to the argument.
|
||||
|
||||
[MessagePack's documentation on the bin type
|
||||
family](https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family) describes this type as:
|
||||
|
||||
@@ -7,12 +7,12 @@ extremely small code sizes, fairly small message size, and extensibility without
|
||||
|
||||
- [CBOR Website](http://cbor.io) - the main source on CBOR
|
||||
- [CBOR Playground](http://cbor.me) - an interactive webpage to translate between JSON and CBOR
|
||||
- [RFC 7049](https://tools.ietf.org/html/rfc7049) - the CBOR specification
|
||||
- [RFC 8949](https://www.rfc-editor.org/rfc/rfc8949.html) - the CBOR specification
|
||||
|
||||
## Serialization
|
||||
|
||||
The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification
|
||||
([RFC 7049](https://www.rfc-editor.org/rfc/rfc7049.html)):
|
||||
([RFC 8949](https://www.rfc-editor.org/rfc/rfc8949.html)):
|
||||
|
||||
| JSON value type | value/range | CBOR type | first byte |
|
||||
|-----------------|--------------------------------------------|-----------------------------------|------------|
|
||||
|
||||
@@ -197,7 +197,10 @@ class binary_reader
|
||||
{
|
||||
const std::size_t document_start = chars_read;
|
||||
std::int32_t document_size{};
|
||||
get_number<std::int32_t, true>(input_format_t::bson, document_size);
|
||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
||||
{
|
||||
@@ -287,7 +290,10 @@ class binary_reader
|
||||
|
||||
// All BSON binary values have a subtype
|
||||
std::uint8_t subtype{};
|
||||
get_number<std::uint8_t>(input_format_t::bson, subtype);
|
||||
if (JSON_HEDLEY_UNLIKELY(!get_number<std::uint8_t>(input_format_t::bson, subtype)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
result.set_subtype(subtype);
|
||||
|
||||
return get_binary(input_format_t::bson, len, result);
|
||||
@@ -340,7 +346,8 @@ class binary_reader
|
||||
|
||||
case 0x08: // boolean
|
||||
{
|
||||
return sax->boolean(get() != 0);
|
||||
std::uint8_t value{};
|
||||
return get_number<std::uint8_t>(input_format_t::bson, value) && sax->boolean(value != 0);
|
||||
}
|
||||
|
||||
case 0x0A: // null
|
||||
@@ -431,7 +438,10 @@ class binary_reader
|
||||
{
|
||||
const std::size_t document_start = chars_read;
|
||||
std::int32_t document_size{};
|
||||
get_number<std::int32_t, true>(input_format_t::bson, document_size);
|
||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
||||
{
|
||||
@@ -896,7 +906,7 @@ class binary_reader
|
||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||
const auto byte2 = static_cast<unsigned char>(byte2_raw);
|
||||
|
||||
// Code from RFC 7049, Appendix D, Figure 3:
|
||||
// Code from RFC 8949, Appendix D, Figure 3:
|
||||
// As half-precision floating-point numbers were only added
|
||||
// to IEEE 754 in 2008, today's programming platforms often
|
||||
// still only have limited support for them. It is very
|
||||
@@ -909,8 +919,8 @@ class binary_reader
|
||||
{
|
||||
const int exp = (half >> 10u) & 0x1Fu;
|
||||
const unsigned int mant = half & 0x3FFu;
|
||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
||||
JSON_ASSERT(mant <= 1024);
|
||||
JSON_ASSERT(exp <= 31);
|
||||
JSON_ASSERT(mant <= 1023);
|
||||
switch (exp)
|
||||
{
|
||||
case 0:
|
||||
@@ -2484,7 +2494,7 @@ class binary_reader
|
||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||
const auto byte2 = static_cast<unsigned char>(byte2_raw);
|
||||
|
||||
// Code from RFC 7049, Appendix D, Figure 3:
|
||||
// Code from RFC 8949, Appendix D, Figure 3:
|
||||
// As half-precision floating-point numbers were only added
|
||||
// to IEEE 754 in 2008, today's programming platforms often
|
||||
// still only have limited support for them. It is very
|
||||
@@ -2497,8 +2507,8 @@ class binary_reader
|
||||
{
|
||||
const int exp = (half >> 10u) & 0x1Fu;
|
||||
const unsigned int mant = half & 0x3FFu;
|
||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
||||
JSON_ASSERT(mant <= 1024);
|
||||
JSON_ASSERT(exp <= 31);
|
||||
JSON_ASSERT(mant <= 1023);
|
||||
switch (exp)
|
||||
{
|
||||
case 0:
|
||||
|
||||
@@ -10746,7 +10746,10 @@ class binary_reader
|
||||
{
|
||||
const std::size_t document_start = chars_read;
|
||||
std::int32_t document_size{};
|
||||
get_number<std::int32_t, true>(input_format_t::bson, document_size);
|
||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
|
||||
{
|
||||
@@ -10836,7 +10839,10 @@ class binary_reader
|
||||
|
||||
// All BSON binary values have a subtype
|
||||
std::uint8_t subtype{};
|
||||
get_number<std::uint8_t>(input_format_t::bson, subtype);
|
||||
if (JSON_HEDLEY_UNLIKELY(!get_number<std::uint8_t>(input_format_t::bson, subtype)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
result.set_subtype(subtype);
|
||||
|
||||
return get_binary(input_format_t::bson, len, result);
|
||||
@@ -10889,7 +10895,8 @@ class binary_reader
|
||||
|
||||
case 0x08: // boolean
|
||||
{
|
||||
return sax->boolean(get() != 0);
|
||||
std::uint8_t value{};
|
||||
return get_number<std::uint8_t>(input_format_t::bson, value) && sax->boolean(value != 0);
|
||||
}
|
||||
|
||||
case 0x0A: // null
|
||||
@@ -10980,7 +10987,10 @@ class binary_reader
|
||||
{
|
||||
const std::size_t document_start = chars_read;
|
||||
std::int32_t document_size{};
|
||||
get_number<std::int32_t, true>(input_format_t::bson, document_size);
|
||||
if (!get_number<std::int32_t, true>(input_format_t::bson, document_size))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
|
||||
{
|
||||
@@ -11445,7 +11455,7 @@ class binary_reader
|
||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||
const auto byte2 = static_cast<unsigned char>(byte2_raw);
|
||||
|
||||
// Code from RFC 7049, Appendix D, Figure 3:
|
||||
// Code from RFC 8949, Appendix D, Figure 3:
|
||||
// As half-precision floating-point numbers were only added
|
||||
// to IEEE 754 in 2008, today's programming platforms often
|
||||
// still only have limited support for them. It is very
|
||||
@@ -11458,8 +11468,8 @@ class binary_reader
|
||||
{
|
||||
const int exp = (half >> 10u) & 0x1Fu;
|
||||
const unsigned int mant = half & 0x3FFu;
|
||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
||||
JSON_ASSERT(mant <= 1024);
|
||||
JSON_ASSERT(exp <= 31);
|
||||
JSON_ASSERT(mant <= 1023);
|
||||
switch (exp)
|
||||
{
|
||||
case 0:
|
||||
@@ -13033,7 +13043,7 @@ class binary_reader
|
||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||
const auto byte2 = static_cast<unsigned char>(byte2_raw);
|
||||
|
||||
// Code from RFC 7049, Appendix D, Figure 3:
|
||||
// Code from RFC 8949, Appendix D, Figure 3:
|
||||
// As half-precision floating-point numbers were only added
|
||||
// to IEEE 754 in 2008, today's programming platforms often
|
||||
// still only have limited support for them. It is very
|
||||
@@ -13046,8 +13056,8 @@ class binary_reader
|
||||
{
|
||||
const int exp = (half >> 10u) & 0x1Fu;
|
||||
const unsigned int mant = half & 0x3FFu;
|
||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
||||
JSON_ASSERT(mant <= 1024);
|
||||
JSON_ASSERT(exp <= 31);
|
||||
JSON_ASSERT(mant <= 1023);
|
||||
switch (exp)
|
||||
{
|
||||
case 0:
|
||||
|
||||
@@ -825,6 +825,41 @@ TEST_CASE("Incomplete BSON Input")
|
||||
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
|
||||
}
|
||||
|
||||
SECTION("Incomplete BSON Input 5")
|
||||
{
|
||||
std::vector<std::uint8_t> const incomplete_bson =
|
||||
{
|
||||
0x09, 0x00, 0x00, 0x00, // size (little endian)
|
||||
0x08, // entry: boolean
|
||||
'b', '\x00' // key, unexpected EOF before the value
|
||||
};
|
||||
|
||||
json _;
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
|
||||
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
|
||||
|
||||
SaxCountdown scp(0);
|
||||
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
|
||||
}
|
||||
|
||||
SECTION("Incomplete BSON Input 6")
|
||||
{
|
||||
std::vector<std::uint8_t> const incomplete_bson =
|
||||
{
|
||||
0x0F, 0x00, 0x00, 0x00, // size (little endian)
|
||||
0x05, // entry: binary
|
||||
'b', '\x00', // key
|
||||
0x00, 0x00, 0x00, 0x00 // length, unexpected EOF before the subtype
|
||||
};
|
||||
|
||||
json _;
|
||||
CHECK_THROWS_WITH_AS(_ = json::from_bson(incomplete_bson), "[json.exception.parse_error.110] parse error at byte 12: syntax error while parsing BSON number: unexpected end of input", json::parse_error&);
|
||||
CHECK(json::from_bson(incomplete_bson, true, false).is_discarded());
|
||||
|
||||
SaxCountdown scp(0);
|
||||
CHECK(!json::sax_parse(incomplete_bson, &scp, json::input_format_t::bson));
|
||||
}
|
||||
|
||||
SECTION("Improve coverage")
|
||||
{
|
||||
SECTION("key")
|
||||
|
||||
@@ -2309,7 +2309,7 @@ TEST_CASE("all CBOR first bytes")
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_CASE("examples from RFC 7049 Appendix A")
|
||||
TEST_CASE("examples from RFC 8949 Appendix A")
|
||||
{
|
||||
SECTION("numbers")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user