@@ -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;
0 commit comments