File tree Expand file tree Collapse file tree 2 files changed +12
-5
lines changed
tools/inspector_protocol/encoding Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -846,10 +846,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
846846 // inspector_protocol, it's not a CBOR limitation); in CBOR, the
847847 // negative values for INT32 are represented as NEGATIVE, that is, -1
848848 // 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()).
853855 if (!success || token_start_internal_value_ >
854856 std::numeric_limits<int32_t >::max ()) {
855857 SetError (Error::CBOR_INVALID_INT32);
Original file line number Diff line number Diff line change @@ -235,7 +235,9 @@ TEST(EncodeDecodeInt32Test, RoundtripsInt32Max) {
235235}
236236
237237TEST (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.
239241 std::vector<uint8_t > encoded;
240242 EncodeInt32 (std::numeric_limits<int32_t >::min (), &encoded);
241243 // 1 for initial byte, 4 for the uint32.
@@ -248,6 +250,9 @@ TEST(EncodeDecodeInt32Test, RoundtripsInt32Min) {
248250 CBORTokenizer tokenizer (SpanFrom (encoded));
249251 EXPECT_EQ (CBORTokenTag::INT32, tokenizer.TokenTag ());
250252 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 ());
251256 tokenizer.Next ();
252257 EXPECT_EQ (CBORTokenTag::DONE, tokenizer.TokenTag ());
253258}
You can’t perform that action at this time.
0 commit comments