Fix CBOR half-float assertion bounds (#5335)
Signed-off-by: Patrick Armstrong <patrick@erpassistant.ai>
This commit is contained in:
1 parent
58ce09dcfd
commit
868506dcc0
5 files changed
+17
-18
No files matched your search
@@ -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`
|
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).
|
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:
|
[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 string's length in bytes is represented following the rules for positive integers
|
> Major type 2: A byte string. The number of bytes in the string is equal to the argument.
|
||||||
> (major type 0).
|
|
||||||
|
|
||||||
[MessagePack's documentation on the bin type
|
[MessagePack's documentation on the bin type
|
||||||
family](https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family) describes this type as:
|
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 Website](http://cbor.io) - the main source on CBOR
|
||||||
- [CBOR Playground](http://cbor.me) - an interactive webpage to translate between JSON and 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
|
## Serialization
|
||||||
|
|
||||||
The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification
|
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 |
|
| JSON value type | value/range | CBOR type | first byte |
|
||||||
|-----------------|--------------------------------------------|-----------------------------------|------------|
|
|-----------------|--------------------------------------------|-----------------------------------|------------|
|
||||||
|
|||||||
@@ -896,7 +896,7 @@ class binary_reader
|
|||||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||||
const auto byte2 = static_cast<unsigned char>(byte2_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
|
// As half-precision floating-point numbers were only added
|
||||||
// to IEEE 754 in 2008, today's programming platforms often
|
// to IEEE 754 in 2008, today's programming platforms often
|
||||||
// still only have limited support for them. It is very
|
// still only have limited support for them. It is very
|
||||||
@@ -909,8 +909,8 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const int exp = (half >> 10u) & 0x1Fu;
|
const int exp = (half >> 10u) & 0x1Fu;
|
||||||
const unsigned int mant = half & 0x3FFu;
|
const unsigned int mant = half & 0x3FFu;
|
||||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
JSON_ASSERT(exp <= 31);
|
||||||
JSON_ASSERT(mant <= 1024);
|
JSON_ASSERT(mant <= 1023);
|
||||||
switch (exp)
|
switch (exp)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
@@ -2484,7 +2484,7 @@ class binary_reader
|
|||||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||||
const auto byte2 = static_cast<unsigned char>(byte2_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
|
// As half-precision floating-point numbers were only added
|
||||||
// to IEEE 754 in 2008, today's programming platforms often
|
// to IEEE 754 in 2008, today's programming platforms often
|
||||||
// still only have limited support for them. It is very
|
// still only have limited support for them. It is very
|
||||||
@@ -2497,8 +2497,8 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const int exp = (half >> 10u) & 0x1Fu;
|
const int exp = (half >> 10u) & 0x1Fu;
|
||||||
const unsigned int mant = half & 0x3FFu;
|
const unsigned int mant = half & 0x3FFu;
|
||||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
JSON_ASSERT(exp <= 31);
|
||||||
JSON_ASSERT(mant <= 1024);
|
JSON_ASSERT(mant <= 1023);
|
||||||
switch (exp)
|
switch (exp)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
@@ -11445,7 +11445,7 @@ class binary_reader
|
|||||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||||
const auto byte2 = static_cast<unsigned char>(byte2_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
|
// As half-precision floating-point numbers were only added
|
||||||
// to IEEE 754 in 2008, today's programming platforms often
|
// to IEEE 754 in 2008, today's programming platforms often
|
||||||
// still only have limited support for them. It is very
|
// still only have limited support for them. It is very
|
||||||
@@ -11458,8 +11458,8 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const int exp = (half >> 10u) & 0x1Fu;
|
const int exp = (half >> 10u) & 0x1Fu;
|
||||||
const unsigned int mant = half & 0x3FFu;
|
const unsigned int mant = half & 0x3FFu;
|
||||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
JSON_ASSERT(exp <= 31);
|
||||||
JSON_ASSERT(mant <= 1024);
|
JSON_ASSERT(mant <= 1023);
|
||||||
switch (exp)
|
switch (exp)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
@@ -13033,7 +13033,7 @@ class binary_reader
|
|||||||
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
const auto byte1 = static_cast<unsigned char>(byte1_raw);
|
||||||
const auto byte2 = static_cast<unsigned char>(byte2_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
|
// As half-precision floating-point numbers were only added
|
||||||
// to IEEE 754 in 2008, today's programming platforms often
|
// to IEEE 754 in 2008, today's programming platforms often
|
||||||
// still only have limited support for them. It is very
|
// still only have limited support for them. It is very
|
||||||
@@ -13046,8 +13046,8 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const int exp = (half >> 10u) & 0x1Fu;
|
const int exp = (half >> 10u) & 0x1Fu;
|
||||||
const unsigned int mant = half & 0x3FFu;
|
const unsigned int mant = half & 0x3FFu;
|
||||||
JSON_ASSERT(0 <= exp&& exp <= 32);
|
JSON_ASSERT(exp <= 31);
|
||||||
JSON_ASSERT(mant <= 1024);
|
JSON_ASSERT(mant <= 1023);
|
||||||
switch (exp)
|
switch (exp)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
@@ -2309,7 +2309,7 @@ TEST_CASE("all CBOR first bytes")
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST_CASE("examples from RFC 7049 Appendix A")
|
TEST_CASE("examples from RFC 8949 Appendix A")
|
||||||
{
|
{
|
||||||
SECTION("numbers")
|
SECTION("numbers")
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user