Skip to content

Commit

Permalink
Fix new crash when indicator includes a newline
Browse files Browse the repository at this point in the history
Fixes new oss-fuzz crash reported in:

  https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=43475

This is in new error handling code, which is why it was not reported
before.
  • Loading branch information
richkadel committed Jan 14, 2022
1 parent ca65fae commit 1b8c904
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json5format"
version = "0.2.3"
version = "0.2.4"
authors = [
"Rich Kadel <richkadel@google.com>",
"David Tamas-Parris <davidatp@google.com>",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
//
26 changes: 20 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,23 @@ impl<'parser> Parser<'parser> {
min_context_len: usize,
ellipsis: &str,
) -> ParserErrorContext {
// `indicator_start` is a 0-based char position
let indicator_start = self.column_number - 1;

let error_line_len = self.current_line.chars().count();

let indicator_len = if self.line_number == self.next_line_number {
std::cmp::max(self.next_column_number - self.column_number, 1)
std::cmp::max(
std::cmp::min(
self.next_column_number - self.column_number,
error_line_len - indicator_start,
),
1,
)
} else {
1
};

// `indicator_start` is a 0-based char position
let indicator_start = self.column_number - 1;

let error_line_len = self.current_line.chars().count();
if error_line_len <= max_error_line_len {
ParserErrorContext::new(self.current_line.to_owned(), indicator_start, indicator_len)
} else {
Expand Down Expand Up @@ -852,7 +859,14 @@ fn trim_error_line_and_indicator(
assert!(max_error_line_len > ellipsis_len);
assert!(max_error_line_len < error_line_len);
assert!(indicator_start <= error_line_len);
assert!(indicator_len == 1 || (indicator_start + indicator_len) <= error_line_len);
assert!(
indicator_len == 1 || (indicator_start + indicator_len) <= error_line_len,
"indicator_start={}, indicator_len={}, error_line_len={}\n{}",
indicator_start,
indicator_len,
error_line_len,
error_line
);

indicator_len = std::cmp::min(indicator_len, max_error_line_len);

Expand Down

0 comments on commit 1b8c904

Please sign in to comment.