Skip to content

Don't abort on an empty input file - #2821

Open
Nishuuzz wants to merge 1 commit into
WebAssembly:mainfrom
Nishuuzz:fix/empty-file-assert
Open

Don't abort on an empty input file#2821
Nishuuzz wants to merge 1 commit into
WebAssembly:mainfrom
Nishuuzz:fix/empty-file-assert

Conversation

@Nishuuzz

Copy link
Copy Markdown
Contributor

An empty input file aborts every tool that formats errors, in any build with assertions on:

$ : > empty.wat
$ wat2wasm empty.wat -o /dev/null
wat2wasm: src/lexer-source-line-finder.cc:30: wabt::LexerSourceLineFinder::LexerSourceLineFinder(...): Assertion `Succeeded(result)' failed.
Aborted

Same for wast2json and wat-desugar.

LexerSource::Seek refuses to seek to the end:

if (offset < size_) {

On a zero-length source that makes Seek(0) fail, and LexerSourceLineFinder's constructor asserts it succeeded. Seek has exactly that one caller, and ReadRange already clamps to size_, so accepting offset == size_ is consistent with the rest of the class — it's simply where an empty source starts, and reading from there yields nothing.

With that, the tools do what they already meant to. wat2wasm warns empty module and writes out an empty module, wast2json warns empty script, and wat-desugar reports no module in file — all of those paths already existed, the assert just fired before they could run.

The test is a unit test rather than one under test/parse, because the harness strips the ;;; directive lines and leaves a newline, so the input is never actually zero bytes. That's also why the existing test/parse/empty-file.txt doesn't catch this — its input is a comment, not nothing.

Found while fuzzing the text front end. For what it's worth, fuzzers/wat2wasm_fuzzer.cc parses but never formats errors, and the line finder is only constructed when formatting, so a zero-length input never reaches it there — which is presumably how this survived.

LexerSource::Seek rejected seeking to the end of the source, so on a
zero-length file Seek(0) failed and the assert in LexerSourceLineFinder's
constructor fired. Every tool that formats errors aborts on an empty
file: wat2wasm, wast2json and wat-desugar.

Seek has one caller, that constructor, and ReadRange already clamps to
size_, so allowing offset == size_ is consistent -- it is where an empty
source starts and reading from it yields nothing.
Comment thread src/test-wast-parser.cc
// by returning, which is what the error formatter already copes with.
LexerSourceLineFinder::SourceLine source_line;
Location loc(1, 1, 1);
EXPECT_EQ(Result::Error, line_finder->GetSourceLine(loc, 80, &source_line));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a file-based test in test/parse/? Is it hard to make a completely empty file there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants