@@ -417,7 +417,7 @@ Result BinaryReader::ReadStr(std::string_view* out_str, const char* desc) {
417417 uint32_t str_len = 0 ;
418418 CHECK_RESULT (ReadU32Leb128 (&str_len, " string length" ));
419419
420- ERROR_UNLESS (state_. offset + str_len <= read_end_,
420+ ERROR_UNLESS (str_len <= read_end_ - state_. offset ,
421421 " unable to read string: %s" , desc);
422422
423423 *out_str = std::string_view (
@@ -442,7 +442,7 @@ Result BinaryReader::ReadBytes(const void** out_data,
442442Result BinaryReader::ReadBytesWithSize (const void ** out_data,
443443 Offset size,
444444 const char * desc) {
445- ERROR_UNLESS (state_. offset + size <= read_end_, " unable to read data: %s" ,
445+ ERROR_UNLESS (size <= read_end_ - state_. offset , " unable to read data: %s" ,
446446 desc);
447447
448448 *out_data = static_cast <const uint8_t *>(state_.data ) + state_.offset ;
@@ -2033,9 +2033,9 @@ Result BinaryReader::ReadNameSection(Offset section_size) {
20332033 }
20342034 previous_subsection_type = name_type;
20352035 CHECK_RESULT (ReadOffset (&subsection_size, " subsection size" ));
2036- size_t subsection_end = state_.offset + subsection_size;
2037- ERROR_UNLESS (subsection_end <= read_end_,
2036+ ERROR_UNLESS (subsection_size <= read_end_ - state_.offset ,
20382037 " invalid sub-section size: extends past end" );
2038+ size_t subsection_end = state_.offset + subsection_size;
20392039 ReadEndRestoreGuard guard (this );
20402040 read_end_ = subsection_end;
20412041
@@ -2224,9 +2224,9 @@ Result BinaryReader::ReadDylink0Section(Offset section_size) {
22242224 Offset subsection_size;
22252225 CHECK_RESULT (ReadU32Leb128 (&dylink_type, " type" ));
22262226 CHECK_RESULT (ReadOffset (&subsection_size, " subsection size" ));
2227- size_t subsection_end = state_.offset + subsection_size;
2228- ERROR_UNLESS (subsection_end <= read_end_,
2227+ ERROR_UNLESS (subsection_size <= read_end_ - state_.offset ,
22292228 " invalid sub-section size: extends past end" );
2229+ size_t subsection_end = state_.offset + subsection_size;
22302230 ReadEndRestoreGuard guard (this );
22312231 read_end_ = subsection_end;
22322232
@@ -2356,9 +2356,9 @@ Result BinaryReader::ReadLinkingSection(Offset section_size) {
23562356 Offset subsection_size;
23572357 CHECK_RESULT (ReadU32Leb128 (&linking_type, " type" ));
23582358 CHECK_RESULT (ReadOffset (&subsection_size, " subsection size" ));
2359- size_t subsection_end = state_.offset + subsection_size;
2360- ERROR_UNLESS (subsection_end <= read_end_,
2359+ ERROR_UNLESS (subsection_size <= read_end_ - state_.offset ,
23612360 " invalid sub-section size: extends past end" );
2361+ size_t subsection_end = state_.offset + subsection_size;
23622362 ReadEndRestoreGuard guard (this );
23632363 read_end_ = subsection_end;
23642364
@@ -3107,6 +3107,8 @@ Result BinaryReader::ReadSections(const ReadSectionsOptions& options) {
31073107 Offset section_size;
31083108 CHECK_RESULT (ReadU8 (§ion_code, " section code" ));
31093109 CHECK_RESULT (ReadOffset (§ion_size, " section size" ));
3110+ ERROR_UNLESS (section_size <= state_.size - state_.offset ,
3111+ " invalid section size: extends past end" );
31103112 ReadEndRestoreGuard guard (this );
31113113 read_end_ = state_.offset + section_size;
31123114 if (section_code >= kBinarySectionCount ) {
0 commit comments