Don't abort on an empty input file - #2821
Open
Nishuuzz wants to merge 1 commit into
Open
Conversation
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.
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?
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 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 existingtest/parse/empty-file.txtdoesn'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.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.