diff --git a/tools/inspector_protocol/encoding/encoding.cc b/tools/inspector_protocol/encoding/encoding.cc index 353316a555373d..7eb499f9712a25 100644 --- a/tools/inspector_protocol/encoding/encoding.cc +++ b/tools/inspector_protocol/encoding/encoding.cc @@ -846,10 +846,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) { // inspector_protocol, it's not a CBOR limitation); in CBOR, the // negative values for INT32 are represented as NEGATIVE, that is, -1 // INT32 is represented as 1 << 5 | 0 (major type 1, additional info - // value 0). The minimal allowed INT32 value in our protocol is - // std::numeric_limits::min(). We check for it by directly - // checking the payload against the maximal allowed signed (!) int32 - // value. + // value 0). + // The represented allowed values range is -1 to -2^31. + // They are mapped into the encoded range of 0 to 2^31-1. + // We check the the payload in token_start_internal_value_ against + // that range (2^31-1 is also known as + // std::numeric_limits::max()). if (!success || token_start_internal_value_ > std::numeric_limits::max()) { SetError(Error::CBOR_INVALID_INT32); diff --git a/tools/inspector_protocol/encoding/encoding_test.cc b/tools/inspector_protocol/encoding/encoding_test.cc index 338d1ece10b87f..067ede2748685a 100644 --- a/tools/inspector_protocol/encoding/encoding_test.cc +++ b/tools/inspector_protocol/encoding/encoding_test.cc @@ -235,7 +235,9 @@ TEST(EncodeDecodeInt32Test, RoundtripsInt32Max) { } TEST(EncodeDecodeInt32Test, RoundtripsInt32Min) { - // std::numeric_limits is encoded as a uint32 after the initial byte. + // std::numeric_limits is encoded as a uint32 (4 unsigned bytes) + // after the initial byte, which effectively carries the sign by + // designating the token as NEGATIVE. std::vector encoded; EncodeInt32(std::numeric_limits::min(), &encoded); // 1 for initial byte, 4 for the uint32. @@ -248,6 +250,9 @@ TEST(EncodeDecodeInt32Test, RoundtripsInt32Min) { CBORTokenizer tokenizer(SpanFrom(encoded)); EXPECT_EQ(CBORTokenTag::INT32, tokenizer.TokenTag()); EXPECT_EQ(std::numeric_limits::min(), tokenizer.GetInt32()); + // It's nice to see how the min int32 value reads in hex: + // That is, -1 minus the unsigned payload (0x7fffffff, see above). + EXPECT_EQ(-0x80000000l, tokenizer.GetInt32()); tokenizer.Next(); EXPECT_EQ(CBORTokenTag::DONE, tokenizer.TokenTag()); }