Skip to content

Commit 8d37874

Browse files
authored
Merge pull request #30 from richkadel/indicator-len-err
Fix new crash when indicator includes a newline
2 parents ca65fae + 1b8c904 commit 8d37874

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json5format"
3-
version = "0.2.3"
3+
version = "0.2.4"
44
authors = [
55
"Rich Kadel <richkadel@google.com>",
66
"David Tamas-Parris <davidatp@google.com>",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[
2+
//

src/parser.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -776,16 +776,23 @@ impl<'parser> Parser<'parser> {
776776
min_context_len: usize,
777777
ellipsis: &str,
778778
) -> ParserErrorContext {
779+
// `indicator_start` is a 0-based char position
780+
let indicator_start = self.column_number - 1;
781+
782+
let error_line_len = self.current_line.chars().count();
783+
779784
let indicator_len = if self.line_number == self.next_line_number {
780-
std::cmp::max(self.next_column_number - self.column_number, 1)
785+
std::cmp::max(
786+
std::cmp::min(
787+
self.next_column_number - self.column_number,
788+
error_line_len - indicator_start,
789+
),
790+
1,
791+
)
781792
} else {
782793
1
783794
};
784795

785-
// `indicator_start` is a 0-based char position
786-
let indicator_start = self.column_number - 1;
787-
788-
let error_line_len = self.current_line.chars().count();
789796
if error_line_len <= max_error_line_len {
790797
ParserErrorContext::new(self.current_line.to_owned(), indicator_start, indicator_len)
791798
} else {
@@ -852,7 +859,14 @@ fn trim_error_line_and_indicator(
852859
assert!(max_error_line_len > ellipsis_len);
853860
assert!(max_error_line_len < error_line_len);
854861
assert!(indicator_start <= error_line_len);
855-
assert!(indicator_len == 1 || (indicator_start + indicator_len) <= error_line_len);
862+
assert!(
863+
indicator_len == 1 || (indicator_start + indicator_len) <= error_line_len,
864+
"indicator_start={}, indicator_len={}, error_line_len={}\n{}",
865+
indicator_start,
866+
indicator_len,
867+
error_line_len,
868+
error_line
869+
);
856870

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

0 commit comments

Comments
 (0)