Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Guard against invalid indices #614

Merged
merged 1 commit into from
Apr 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/render/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::render::{
};
use anyhow::Result;
use console::{Color, Style, Term};
use log::{debug, info};
use log::{debug, error, info};
use serde::{Deserialize, Serialize};
use std::{cmp::max, io::Write};

Expand Down Expand Up @@ -248,10 +248,21 @@ impl Unified {
self.print_hunk_title(term, hunk, fmt)?;

for line in &hunk.0 {
let text = lines[line.line_index];
debug!("Printing line {}", line.line_index);
let line_index = line.line_index;
// It's find for this to be fatal in debug builds. We want to avoid crashing in
// release.
debug_assert!(line_index < lines.len());
if line_index >= lines.len() {
error!(
"Received invalid line index {}. Skipping printing this line.",
line_index
);
continue;
}
let text = lines[line_index];
debug!("Printing line {}", line_index);
self.print_line(term, text, line, fmt)?;
debug!("End line {}", line.line_index);
debug!("End line {}", line_index);
}
debug!(
"End hunk (lines {} - {})",
Expand Down