@@ -202,6 +202,7 @@ class BinaryReader {
202202 Index num_tag_imports_ = 0 ;
203203 Index num_function_signatures_ = 0 ;
204204 Index num_function_bodies_ = 0 ;
205+ Index num_data_segments_ = 0 ;
205206 Index data_count_ = kInvalidIndex ;
206207
207208 using ReadEndRestoreGuard =
@@ -2829,13 +2830,13 @@ Result BinaryReader::ReadCodeSection(Offset section_size) {
28292830
28302831Result BinaryReader::ReadDataSection (Offset section_size) {
28312832 CALLBACK (BeginDataSection, section_size);
2832- Index num_data_segments;
2833- CHECK_RESULT (ReadCount (&num_data_segments, " data segment count" ));
2834- CALLBACK (OnDataSegmentCount, num_data_segments);
2833+ CHECK_RESULT (ReadCount (&num_data_segments_, " data segment count" ));
2834+ CALLBACK (OnDataSegmentCount, num_data_segments_);
28352835 // If the DataCount section is not present, then data_count_ will be invalid.
2836- ERROR_UNLESS (data_count_ == kInvalidIndex || data_count_ == num_data_segments,
2837- " data segment count does not equal count in DataCount section" );
2838- for (Index i = 0 ; i < num_data_segments; ++i) {
2836+ ERROR_UNLESS (
2837+ data_count_ == kInvalidIndex || data_count_ == num_data_segments_,
2838+ " data segment count does not equal count in DataCount section" );
2839+ for (Index i = 0 ; i < num_data_segments_; ++i) {
28392840 uint32_t flags;
28402841 CHECK_RESULT (ReadU32Leb128 (&flags, " data segment flags" ));
28412842 ERROR_IF (flags != 0 && !options_.features .bulk_memory_enabled (),
@@ -3037,6 +3038,10 @@ Result BinaryReader::ReadModule(const ReadModuleOptions& options) {
30373038 // in case the code section was omitted.
30383039 ERROR_UNLESS (num_function_signatures_ == num_function_bodies_,
30393040 " function signature count != function body count" );
3041+ // This is checked in ReadDataSection, but it must be checked at the end too,
3042+ // in case the data section was omitted.
3043+ ERROR_IF (num_data_segments_ == 0 && data_count_ != kInvalidIndex ,
3044+ " Data section missing but DataCount non-zero" );
30403045 CALLBACK0 (EndModule);
30413046
30423047 return Result::Ok;
0 commit comments