Skip to content

Commit ee7142f

Browse files
Trottdanielleadams
authored andcommitted
tools: update inspector_protocol to 97d3146
To apply the diff cleanly, ffb34b6 had to be reverted. Refs: https://chromium.googlesource.com/deps/inspector_protocol/+log PR-URL: #39694 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c6323d8 commit ee7142f

File tree

4 files changed

+50
-56
lines changed

4 files changed

+50
-56
lines changed

tools/inspector_protocol/encoding/encoding.cc

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,10 @@ namespace internals {
185185
// |type| is the major type as specified in RFC 7049 Section 2.1.
186186
// |value| is the payload (e.g. for MajorType::UNSIGNED) or is the size
187187
// (e.g. for BYTE_STRING).
188-
// If successful, returns the number of bytes read. Otherwise returns -1.
189-
// TODO(johannes): change return type to size_t and use 0 for error.
190-
int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) {
188+
// If successful, returns the number of bytes read. Otherwise returns 0.
189+
size_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) {
191190
if (bytes.empty())
192-
return -1;
191+
return 0;
193192
uint8_t initial_byte = bytes[0];
194193
*type = MajorType((initial_byte & kMajorTypeMask) >> kMajorTypeBitShift);
195194

@@ -203,32 +202,32 @@ int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) {
203202
if (additional_information == kAdditionalInformation1Byte) {
204203
// Values 24-255 are encoded with one initial byte, followed by the value.
205204
if (bytes.size() < 2)
206-
return -1;
205+
return 0;
207206
*value = ReadBytesMostSignificantByteFirst<uint8_t>(bytes.subspan(1));
208207
return 2;
209208
}
210209
if (additional_information == kAdditionalInformation2Bytes) {
211210
// Values 256-65535: 1 initial byte + 2 bytes payload.
212211
if (bytes.size() < 1 + sizeof(uint16_t))
213-
return -1;
212+
return 0;
214213
*value = ReadBytesMostSignificantByteFirst<uint16_t>(bytes.subspan(1));
215214
return 3;
216215
}
217216
if (additional_information == kAdditionalInformation4Bytes) {
218217
// 32 bit uint: 1 initial byte + 4 bytes payload.
219218
if (bytes.size() < 1 + sizeof(uint32_t))
220-
return -1;
219+
return 0;
221220
*value = ReadBytesMostSignificantByteFirst<uint32_t>(bytes.subspan(1));
222221
return 5;
223222
}
224223
if (additional_information == kAdditionalInformation8Bytes) {
225224
// 64 bit uint: 1 initial byte + 8 bytes payload.
226225
if (bytes.size() < 1 + sizeof(uint64_t))
227-
return -1;
226+
return 0;
228227
*value = ReadBytesMostSignificantByteFirst<uint64_t>(bytes.subspan(1));
229228
return 9;
230229
}
231-
return -1;
230+
return 0;
232231
}
233232

234233
// Writes the start of a token with |type|. The |value| may indicate the size,
@@ -770,10 +769,10 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
770769
SetToken(CBORTokenTag::NULL_VALUE, 1);
771770
return;
772771
case kExpectedConversionToBase64Tag: { // BINARY
773-
const int8_t bytes_read = internals::ReadTokenStart(
772+
const size_t bytes_read = internals::ReadTokenStart(
774773
bytes_.subspan(status_.pos + 1), &token_start_type_,
775774
&token_start_internal_value_);
776-
if (bytes_read < 0 || token_start_type_ != MajorType::BYTE_STRING ||
775+
if (!bytes_read || token_start_type_ != MajorType::BYTE_STRING ||
777776
token_start_internal_value_ > kMaxValidLength) {
778777
SetError(Error::CBOR_INVALID_BINARY);
779778
return;
@@ -823,22 +822,21 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
823822
return;
824823
}
825824
default: {
826-
const int8_t token_start_length = internals::ReadTokenStart(
825+
const size_t bytes_read = internals::ReadTokenStart(
827826
bytes_.subspan(status_.pos), &token_start_type_,
828827
&token_start_internal_value_);
829-
const bool success = token_start_length >= 0;
830828
switch (token_start_type_) {
831829
case MajorType::UNSIGNED: // INT32.
832830
// INT32 is a signed int32 (int32 makes sense for the
833831
// inspector_protocol, it's not a CBOR limitation), so we check
834832
// against the signed max, so that the allowable values are
835833
// 0, 1, 2, ... 2^31 - 1.
836-
if (!success || std::numeric_limits<int32_t>::max() <
837-
token_start_internal_value_) {
834+
if (!bytes_read || std::numeric_limits<int32_t>::max() <
835+
token_start_internal_value_) {
838836
SetError(Error::CBOR_INVALID_INT32);
839837
return;
840838
}
841-
SetToken(CBORTokenTag::INT32, token_start_length);
839+
SetToken(CBORTokenTag::INT32, bytes_read);
842840
return;
843841
case MajorType::NEGATIVE: { // INT32.
844842
// INT32 is a signed int32 (int32 makes sense for the
@@ -851,21 +849,20 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
851849
// We check the the payload in token_start_internal_value_ against
852850
// that range (2^31-1 is also known as
853851
// std::numeric_limits<int32_t>::max()).
854-
if (!success || token_start_internal_value_ >
855-
std::numeric_limits<int32_t>::max()) {
852+
if (!bytes_read || token_start_internal_value_ >
853+
std::numeric_limits<int32_t>::max()) {
856854
SetError(Error::CBOR_INVALID_INT32);
857855
return;
858856
}
859-
SetToken(CBORTokenTag::INT32, token_start_length);
857+
SetToken(CBORTokenTag::INT32, bytes_read);
860858
return;
861859
}
862860
case MajorType::STRING: { // STRING8.
863-
if (!success || token_start_internal_value_ > kMaxValidLength) {
861+
if (!bytes_read || token_start_internal_value_ > kMaxValidLength) {
864862
SetError(Error::CBOR_INVALID_STRING8);
865863
return;
866864
}
867-
uint64_t token_byte_length =
868-
token_start_internal_value_ + token_start_length;
865+
uint64_t token_byte_length = token_start_internal_value_ + bytes_read;
869866
if (token_byte_length > remaining_bytes) {
870867
SetError(Error::CBOR_INVALID_STRING8);
871868
return;
@@ -877,13 +874,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
877874
case MajorType::BYTE_STRING: { // STRING16.
878875
// Length must be divisible by 2 since UTF16 is 2 bytes per
879876
// character, hence the &1 check.
880-
if (!success || token_start_internal_value_ > kMaxValidLength ||
877+
if (!bytes_read || token_start_internal_value_ > kMaxValidLength ||
881878
token_start_internal_value_ & 1) {
882879
SetError(Error::CBOR_INVALID_STRING16);
883880
return;
884881
}
885-
uint64_t token_byte_length =
886-
token_start_internal_value_ + token_start_length;
882+
uint64_t token_byte_length = token_start_internal_value_ + bytes_read;
887883
if (token_byte_length > remaining_bytes) {
888884
SetError(Error::CBOR_INVALID_STRING16);
889885
return;

tools/inspector_protocol/encoding/encoding.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ Status AppendString8EntryToCBORMap(span<uint8_t> string8_key,
427427
std::string* cbor);
428428

429429
namespace internals { // Exposed only for writing tests.
430-
int8_t ReadTokenStart(span<uint8_t> bytes,
430+
size_t ReadTokenStart(span<uint8_t> bytes,
431431
cbor::MajorType* type,
432432
uint64_t* value);
433433

tools/inspector_protocol/lib/encoding_cpp.template

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,10 @@ namespace internals {
192192
// |type| is the major type as specified in RFC 7049 Section 2.1.
193193
// |value| is the payload (e.g. for MajorType::UNSIGNED) or is the size
194194
// (e.g. for BYTE_STRING).
195-
// If successful, returns the number of bytes read. Otherwise returns -1.
196-
// TODO(johannes): change return type to size_t and use 0 for error.
197-
int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) {
195+
// If successful, returns the number of bytes read. Otherwise returns 0.
196+
size_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) {
198197
if (bytes.empty())
199-
return -1;
198+
return 0;
200199
uint8_t initial_byte = bytes[0];
201200
*type = MajorType((initial_byte & kMajorTypeMask) >> kMajorTypeBitShift);
202201

@@ -210,32 +209,32 @@ int8_t ReadTokenStart(span<uint8_t> bytes, MajorType* type, uint64_t* value) {
210209
if (additional_information == kAdditionalInformation1Byte) {
211210
// Values 24-255 are encoded with one initial byte, followed by the value.
212211
if (bytes.size() < 2)
213-
return -1;
212+
return 0;
214213
*value = ReadBytesMostSignificantByteFirst<uint8_t>(bytes.subspan(1));
215214
return 2;
216215
}
217216
if (additional_information == kAdditionalInformation2Bytes) {
218217
// Values 256-65535: 1 initial byte + 2 bytes payload.
219218
if (bytes.size() < 1 + sizeof(uint16_t))
220-
return -1;
219+
return 0;
221220
*value = ReadBytesMostSignificantByteFirst<uint16_t>(bytes.subspan(1));
222221
return 3;
223222
}
224223
if (additional_information == kAdditionalInformation4Bytes) {
225224
// 32 bit uint: 1 initial byte + 4 bytes payload.
226225
if (bytes.size() < 1 + sizeof(uint32_t))
227-
return -1;
226+
return 0;
228227
*value = ReadBytesMostSignificantByteFirst<uint32_t>(bytes.subspan(1));
229228
return 5;
230229
}
231230
if (additional_information == kAdditionalInformation8Bytes) {
232231
// 64 bit uint: 1 initial byte + 8 bytes payload.
233232
if (bytes.size() < 1 + sizeof(uint64_t))
234-
return -1;
233+
return 0;
235234
*value = ReadBytesMostSignificantByteFirst<uint64_t>(bytes.subspan(1));
236235
return 9;
237236
}
238-
return -1;
237+
return 0;
239238
}
240239

241240
// Writes the start of a token with |type|. The |value| may indicate the size,
@@ -777,10 +776,10 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
777776
SetToken(CBORTokenTag::NULL_VALUE, 1);
778777
return;
779778
case kExpectedConversionToBase64Tag: { // BINARY
780-
const int8_t bytes_read = internals::ReadTokenStart(
779+
const size_t bytes_read = internals::ReadTokenStart(
781780
bytes_.subspan(status_.pos + 1), &token_start_type_,
782781
&token_start_internal_value_);
783-
if (bytes_read < 0 || token_start_type_ != MajorType::BYTE_STRING ||
782+
if (!bytes_read || token_start_type_ != MajorType::BYTE_STRING ||
784783
token_start_internal_value_ > kMaxValidLength) {
785784
SetError(Error::CBOR_INVALID_BINARY);
786785
return;
@@ -830,47 +829,47 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
830829
return;
831830
}
832831
default: {
833-
const int8_t token_start_length = internals::ReadTokenStart(
832+
const size_t bytes_read = internals::ReadTokenStart(
834833
bytes_.subspan(status_.pos), &token_start_type_,
835834
&token_start_internal_value_);
836-
const bool success = token_start_length >= 0;
837835
switch (token_start_type_) {
838836
case MajorType::UNSIGNED: // INT32.
839837
// INT32 is a signed int32 (int32 makes sense for the
840838
// inspector_protocol, it's not a CBOR limitation), so we check
841839
// against the signed max, so that the allowable values are
842840
// 0, 1, 2, ... 2^31 - 1.
843-
if (!success || std::numeric_limits<int32_t>::max() <
844-
token_start_internal_value_) {
841+
if (!bytes_read || std::numeric_limits<int32_t>::max() <
842+
token_start_internal_value_) {
845843
SetError(Error::CBOR_INVALID_INT32);
846844
return;
847845
}
848-
SetToken(CBORTokenTag::INT32, token_start_length);
846+
SetToken(CBORTokenTag::INT32, bytes_read);
849847
return;
850848
case MajorType::NEGATIVE: { // INT32.
851849
// INT32 is a signed int32 (int32 makes sense for the
852850
// inspector_protocol, it's not a CBOR limitation); in CBOR, the
853851
// negative values for INT32 are represented as NEGATIVE, that is, -1
854852
// INT32 is represented as 1 << 5 | 0 (major type 1, additional info
855-
// value 0). The minimal allowed INT32 value in our protocol is
856-
// std::numeric_limits<int32_t>::min(). We check for it by directly
857-
// checking the payload against the maximal allowed signed (!) int32
858-
// value.
859-
if (!success || token_start_internal_value_ >
860-
std::numeric_limits<int32_t>::max()) {
853+
// value 0).
854+
// The represented allowed values range is -1 to -2^31.
855+
// They are mapped into the encoded range of 0 to 2^31-1.
856+
// We check the the payload in token_start_internal_value_ against
857+
// that range (2^31-1 is also known as
858+
// std::numeric_limits<int32_t>::max()).
859+
if (!bytes_read || token_start_internal_value_ >
860+
std::numeric_limits<int32_t>::max()) {
861861
SetError(Error::CBOR_INVALID_INT32);
862862
return;
863863
}
864-
SetToken(CBORTokenTag::INT32, token_start_length);
864+
SetToken(CBORTokenTag::INT32, bytes_read);
865865
return;
866866
}
867867
case MajorType::STRING: { // STRING8.
868-
if (!success || token_start_internal_value_ > kMaxValidLength) {
868+
if (!bytes_read || token_start_internal_value_ > kMaxValidLength) {
869869
SetError(Error::CBOR_INVALID_STRING8);
870870
return;
871871
}
872-
uint64_t token_byte_length =
873-
token_start_internal_value_ + token_start_length;
872+
uint64_t token_byte_length = token_start_internal_value_ + bytes_read;
874873
if (token_byte_length > remaining_bytes) {
875874
SetError(Error::CBOR_INVALID_STRING8);
876875
return;
@@ -882,13 +881,12 @@ void CBORTokenizer::ReadNextToken(bool enter_envelope) {
882881
case MajorType::BYTE_STRING: { // STRING16.
883882
// Length must be divisible by 2 since UTF16 is 2 bytes per
884883
// character, hence the &1 check.
885-
if (!success || token_start_internal_value_ > kMaxValidLength ||
884+
if (!bytes_read || token_start_internal_value_ > kMaxValidLength ||
886885
token_start_internal_value_ & 1) {
887886
SetError(Error::CBOR_INVALID_STRING16);
888887
return;
889888
}
890-
uint64_t token_byte_length =
891-
token_start_internal_value_ + token_start_length;
889+
uint64_t token_byte_length = token_start_internal_value_ + bytes_read;
892890
if (token_byte_length > remaining_bytes) {
893891
SetError(Error::CBOR_INVALID_STRING16);
894892
return;

tools/inspector_protocol/lib/encoding_h.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ Status AppendString8EntryToCBORMap(span<uint8_t> string8_key,
435435
std::string* cbor);
436436

437437
namespace internals { // Exposed only for writing tests.
438-
int8_t ReadTokenStart(span<uint8_t> bytes,
438+
size_t ReadTokenStart(span<uint8_t> bytes,
439439
cbor::MajorType* type,
440440
uint64_t* value);
441441

0 commit comments

Comments
 (0)