From e9176b248862dedccd59a9344c9b4b6a219652b3 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 18:57:27 +0200 Subject: [PATCH 1/3] fix: handle out of bounds access in PathList display Fixes #309 Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- crates/core/src/repofile/snapshotfile.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/core/src/repofile/snapshotfile.rs b/crates/core/src/repofile/snapshotfile.rs index 5141aa54..481474fa 100644 --- a/crates/core/src/repofile/snapshotfile.rs +++ b/crates/core/src/repofile/snapshotfile.rs @@ -1196,9 +1196,14 @@ impl Display for PathList { if !self.0.is_empty() { write!(f, "{:?}", self.0[0])?; } - for p in &self.0[1..] { - write!(f, ",{p:?}")?; + + // Guard against out of bounds access + if self.0.len() > 1 { + for p in &self.0[1..] { + write!(f, ",{p:?}")?; + } } + Ok(()) } } From 13efb2e30cdb650ead75df6ac6ef5827d42ced67 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 5 Oct 2024 20:07:56 +0200 Subject: [PATCH 2/3] use different implementation and impl tests Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- crates/core/src/repofile/snapshotfile.rs | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/core/src/repofile/snapshotfile.rs b/crates/core/src/repofile/snapshotfile.rs index 481474fa..8bfbc05a 100644 --- a/crates/core/src/repofile/snapshotfile.rs +++ b/crates/core/src/repofile/snapshotfile.rs @@ -1193,16 +1193,14 @@ pub struct PathList(Vec); impl Display for PathList { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if !self.0.is_empty() { - write!(f, "{:?}", self.0[0])?; - } + let string = self + .0 + .iter() + .map(|p| p.to_string_lossy()) + .collect::>() + .join(","); - // Guard against out of bounds access - if self.0.len() > 1 { - for p in &self.0[1..] { - write!(f, ",{p:?}")?; - } - } + write!(f, "{string}")?; Ok(()) } @@ -1373,4 +1371,14 @@ mod tests { assert_eq!(crit.paths, is_path); assert_eq!(crit.tags, is_tags); } + + #[rstest] + #[case(vec![], "")] + #[case(vec!["test"], "test")] + #[case(vec!["test", "test", "test"], "test,test,test")] + fn test_display_path_list_passes(#[case] input: Vec<&str>, #[case] expected: &str) { + let path_list = PathList::from_iter(input); + let result = path_list.to_string(); + assert_eq!(expected, &result); + } } From dcc2e73f96ea993515054a7b91c1b2dd1cca2b76 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Sun, 6 Oct 2024 21:55:21 +0200 Subject: [PATCH 3/3] use itertools::format --- crates/core/src/repofile/snapshotfile.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/crates/core/src/repofile/snapshotfile.rs b/crates/core/src/repofile/snapshotfile.rs index 8bfbc05a..a973bfce 100644 --- a/crates/core/src/repofile/snapshotfile.rs +++ b/crates/core/src/repofile/snapshotfile.rs @@ -1193,16 +1193,11 @@ pub struct PathList(Vec); impl Display for PathList { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let string = self - .0 + self.0 .iter() .map(|p| p.to_string_lossy()) - .collect::>() - .join(","); - - write!(f, "{string}")?; - - Ok(()) + .format(",") + .fmt(f) } }