Index: llvm/trunk/lib/Object/WasmObjectFile.cpp =================================================================== --- llvm/trunk/lib/Object/WasmObjectFile.cpp +++ llvm/trunk/lib/Object/WasmObjectFile.cpp @@ -178,14 +178,16 @@ } static Error readSection(WasmSection &Section, const uint8_t *&Ptr, - const uint8_t *Start) { - // TODO(sbc): Avoid reading past EOF in the case of malformed files. + const uint8_t *Start, const uint8_t *Eof) { Section.Offset = Ptr - Start; Section.Type = readVaruint7(Ptr); uint32_t Size = readVaruint32(Ptr); if (Size == 0) return make_error("Zero length section", object_error::parse_failed); + if (Ptr + Size > Eof) + return make_error("Section too large", + object_error::parse_failed); Section.Content = ArrayRef(Ptr, Size); Ptr += Size; return Error::success(); @@ -221,7 +223,7 @@ WasmSection Sec; while (Ptr < Eof) { - if ((Err = readSection(Sec, Ptr, getPtr(0)))) + if ((Err = readSection(Sec, Ptr, getPtr(0), Eof))) return; if ((Err = parseSection(Sec))) return; Index: llvm/trunk/test/tools/llvm-objdump/wasm-corrupt-section.test =================================================================== --- llvm/trunk/test/tools/llvm-objdump/wasm-corrupt-section.test +++ llvm/trunk/test/tools/llvm-objdump/wasm-corrupt-section.test @@ -0,0 +1,2 @@ +# RUN: not llvm-objdump -h %p/Inputs/corrupt-section.wasm 2>&1 | FileCheck %s +# CHECK: '{{.*}}corrupt-section.wasm': Section too large