diff options
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 4 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 1 | ||||
-rw-r--r-- | test/lit/parse-bad-tuple-extract-index.wast | 16 |
3 files changed, 21 insertions, 0 deletions
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index c1d25f10d..f3ab50f0c 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -2527,6 +2527,10 @@ Expression* SExpressionWasmBuilder::makeTupleExtract(Element& s) { auto ret = allocator.alloc<TupleExtract>(); ret->index = atoi(s[1]->str().c_str()); ret->tuple = parseExpression(s[2]); + if (ret->tuple->type != Type::unreachable && + ret->index >= ret->tuple->type.size()) { + throw ParseException("Bad index on tuple.extract", s[1]->line, s[1]->col); + } ret->finalize(); return ret; } diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 18f8594b1..2ccd9a70a 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -872,6 +872,7 @@ void TupleExtract::finalize() { if (tuple->type == Type::unreachable) { type = Type::unreachable; } else { + assert(index < tuple->type.size()); type = tuple->type[index]; } } diff --git a/test/lit/parse-bad-tuple-extract-index.wast b/test/lit/parse-bad-tuple-extract-index.wast new file mode 100644 index 000000000..01974fe7e --- /dev/null +++ b/test/lit/parse-bad-tuple-extract-index.wast @@ -0,0 +1,16 @@ +;; Test that out-of-bounds tuple.extract indices produce parse errors. + +;; RUN: not wasm-opt %s 2>&1 | filecheck %s + +;; CHECK: [parse exception: Bad index on tuple.extract (at 9:17)] + +(module + (func + (tuple.extract 2 + (tuple.make + (i32.const 0) + (i64.const 1) + ) + ) + ) +) |