Skip to content

Commit 3852498

Browse files
authored
Fix handling of data count without data section (#2432)
Closes #2436 Fixes #2310 Fixes #2311 Fixes #2431
1 parent 3fd8c70 commit 3852498

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/binary-reader.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

28302831
Result 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;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;;; TOOL: run-interp-spec
2+
(assert_malformed
3+
(module binary
4+
"\00asm" "\01\00\00\00"
5+
"\05\03\01\00\01" ;; Memory section with one entry
6+
"\0c\01\01" ;; Data count section with value 1
7+
)
8+
"data count and data section have inconsistent lengths"
9+
)
10+
(;; STDOUT ;;;
11+
out/test/regress/data-count-without-data-section.txt:3: assert_malformed passed:
12+
0000010: error: Data section missing but DataCount non-zero
13+
1/1 tests passed.
14+
;;; STDOUT ;;)

0 commit comments

Comments
 (0)