Skip to content

Commit 9dd3ab4

Browse files
authored
Fix a panic rendering a text parsing error (#2266)
* Fix a panic rendering a text parsing error Don't try to print a huge amount of whitespace to a column, instead cap the size to a reasonable value. Closes #2262 * Fix test failure on Windows
1 parent f06c721 commit 9dd3ab4

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

crates/wast/src/error.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl fmt::Display for Error {
138138
let text = match &self.inner.text {
139139
Some(text) => text,
140140
None => {
141-
return write!(f, "{} at byte offset {}", err, self.inner.span.offset);
141+
return write!(f, "{err} at byte offset {}", self.inner.span.offset);
142142
}
143143
};
144144
let file = self
@@ -147,6 +147,14 @@ impl fmt::Display for Error {
147147
.as_ref()
148148
.and_then(|p| p.to_str())
149149
.unwrap_or("<anon>");
150+
let col = text.col + 1;
151+
let line = text.line + 1;
152+
153+
// If the column is too big skip the fancy rendering below and just
154+
// print the raw error.
155+
if col > 500 {
156+
return write!(f, "{err} at {file}:{line}:{col}");
157+
}
150158
write!(
151159
f,
152160
"\
@@ -155,11 +163,7 @@ impl fmt::Display for Error {
155163
|
156164
{line:4} | {text}
157165
| {marker:>0$}",
158-
text.col + 1,
159-
file = file,
160-
line = text.line + 1,
161-
col = text.col + 1,
162-
err = err,
166+
col,
163167
text = text.snippet,
164168
marker = "^",
165169
)

tests/cli/failure-at-very-large-column.wat

Lines changed: 8 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error: unexpected token, expected one of: `i32`, `i64`, `f32`, `f64`, `v128`, reftype at tests/cli/failure-at-very-large-column.wat:8:192584

0 commit comments

Comments
 (0)