| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 61c53f3 commit 6e19c16
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -846,10 +846,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { | |||
| 846 | 846 | // inspector_protocol, it's not a CBOR limitation); in CBOR, the | |
| 847 | 847 | // negative values for INT32 are represented as NEGATIVE, that is, -1 | |
| 848 | 848 | // INT32 is represented as 1 << 5 | 0 (major type 1, additional info | |
| 849 | - // value 0). The minimal allowed INT32 value in our protocol is | ||
| 850 | - // std::numeric_limits<int32_t>::min(). We check for it by directly | ||
| 851 | - // checking the payload against the maximal allowed signed (!) int32 | ||
| 852 | - // value. | ||
| 849 | + // value 0). | ||
| 850 | + // The represented allowed values range is -1 to -2^31. | ||
| 851 | + // They are mapped into the encoded range of 0 to 2^31-1. | ||
| 852 | + // We check the the payload in token_start_internal_value_ against | ||
| 853 | + // that range (2^31-1 is also known as | ||
| 854 | + // std::numeric_limits<int32_t>::max()). | ||
| 853 | 855 | if (!success || token_start_internal_value_ > | |
| 854 | 856 | std::numeric_limits<int32_t>::max()) { | |
| 855 | 857 | SetError(Error::CBOR_INVALID_INT32); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -235,7 +235,9 @@ TEST(EncodeDecodeInt32Test, RoundtripsInt32Max) { | |||
| 235 | 235 | } | |
| 236 | 236 | ||
| 237 | 237 | TEST(EncodeDecodeInt32Test, RoundtripsInt32Min) { | |
| 238 | - // std::numeric_limits<int32_t> is encoded as a uint32 after the initial byte. | ||
| 238 | + // std::numeric_limits<int32_t> is encoded as a uint32 (4 unsigned bytes) | ||
| 239 | + // after the initial byte, which effectively carries the sign by | ||
| 240 | + // designating the token as NEGATIVE. | ||
| 239 | 241 | std::vector<uint8_t> encoded; | |
| 240 | 242 | EncodeInt32(std::numeric_limits<int32_t>::min(), &encoded); | |
| 241 | 243 | // 1 for initial byte, 4 for the uint32. | |
@@ -248,6 +250,9 @@ TEST(EncodeDecodeInt32Test, RoundtripsInt32Min) { | |||
| 248 | 250 | CBORTokenizer tokenizer(SpanFrom(encoded)); | |
| 249 | 251 | EXPECT_EQ(CBORTokenTag::INT32, tokenizer.TokenTag()); | |
| 250 | 252 | EXPECT_EQ(std::numeric_limits<int32_t>::min(), tokenizer.GetInt32()); | |
| 253 | + // It's nice to see how the min int32 value reads in hex: | ||
| 254 | + // That is, -1 minus the unsigned payload (0x7fffffff, see above). | ||
| 255 | + EXPECT_EQ(-0x80000000l, tokenizer.GetInt32()); | ||
| 251 | 256 | tokenizer.Next(); | |
| 252 | 257 | EXPECT_EQ(CBORTokenTag::DONE, tokenizer.TokenTag()); | |
| 253 | 258 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments