Skip to content

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

Merged
sbc100 merged 1 commit into
WebAssembly:mainfrom
Nishuuzz:fix/empty-file-assert
Aug 13, 2026
Merged

sbc100 merged 1 commit into
WebAssembly:mainfrom
Nishuuzz:fix/empty-file-assert

Conversation

@Nishuuzz

@Nishuuzz Nishuuzz commented Aug 12, 2026 •

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 test/parse/zero-length-file.txt, using STDIN_FILE to point at a checked-in zero byte file. Writing the input inline doesn't work for this: CreateInputFile pads the input with one newline per header line so the reported line numbers line up, so the file is never actually empty. That's also why the existing test/parse/empty-file.txt doesn't catch this — its input is a comment, not nothing. Without the fix the new test fails with expected error code 0, got -6.

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.

Comment thread src/test-wast-parser.cc Outdated
// 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?

@Nishuuzz Nishuuzz Aug 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it can be done, switched to test/parse/zero-length-file.txt and dropped the unit test.

The trick is STDIN_FILE, pointing at a checked-in zero byte file. Writing the input inline is what does not work: CreateInputFile pads it with one newline per header line so the reported line numbers match up, so the input is a single newline rather than nothing. With STDIN_FILE the file is copied through verbatim.

Confirmed it actually catches the bug — without the fix it fails with expected error code 0, got -6 and the assertion message.

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.
@Nishuuzz
Nishuuzz force-pushed the fix/empty-file-assert branch from aeb4dcc to 10047f4 Compare August 13, 2026 07:11
@sbc100
sbc100 merged commit 671b860 into WebAssembly:main Aug 13, 2026
17 checks passed
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