Repository navigation
Don't abort on an empty input file - #2821
Merged
Merged
Conversation
sbc100
reviewed
Aug 12, 2026
| // 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)); |
Member
There was a problem hiding this comment.
Can this be a file-based test in test/parse/? Is it hard to make a completely empty file there?
Contributor
Author
There was a problem hiding this comment.
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
force-pushed
the
fix/empty-file-assert
branch
from
August 13, 2026 07:11
aeb4dcc to
10047f4
Compare
sbc100
approved these changes
Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An empty input file aborts every tool that formats errors, in any build with assertions on:
Same for
wast2jsonandwat-desugar.LexerSource::Seekrefuses to seek to the end:if (offset < size_) {On a zero-length source that makes
Seek(0)fail, andLexerSourceLineFinder's constructor asserts it succeeded.Seekhas exactly that one caller, andReadRangealready clamps tosize_, so acceptingoffset == 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.
wat2wasmwarnsempty moduleand writes out an empty module,wast2jsonwarnsempty script, andwat-desugarreportsno 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, usingSTDIN_FILEto point at a checked-in zero byte file. Writing the input inline doesn't work for this:CreateInputFilepads 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 existingtest/parse/empty-file.txtdoesn't catch this — its input is a comment, not nothing. Without the fix the new test fails withexpected error code 0, got -6.Found while fuzzing the text front end. For what it's worth,
fuzzers/wat2wasm_fuzzer.ccparses 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.