From 4db70096407e808b42e15f8a9cb7bf9eecacc35d Mon Sep 17 00:00:00 2001 From: James Wilson Date: Tue, 29 Jul 2025 14:12:16 +0100 Subject: [PATCH] Ensure path in corpus is relative to corpus file (#85) --- crates/format/src/corpus.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/format/src/corpus.rs b/crates/format/src/corpus.rs index 80071b3..43e07dc 100644 --- a/crates/format/src/corpus.rs +++ b/crates/format/src/corpus.rs @@ -17,7 +17,25 @@ impl Corpus { /// Try to read and parse the corpus definition file at given `path`. pub fn try_from_path(path: &Path) -> anyhow::Result { let file = File::open(path)?; - Ok(serde_json::from_reader(file)?) + let mut corpus: Corpus = serde_json::from_reader(file)?; + + // Ensure that the path mentioned in the corpus is relative to the corpus file. + // Canonicalizing also helps make the path in any errors unambiguous. + corpus.path = path + .parent() + .ok_or_else(|| { + anyhow::anyhow!("Corpus path '{}' does not point to a file", path.display()) + })? + .canonicalize() + .map_err(|error| { + anyhow::anyhow!( + "Failed to canonicalize path to corpus '{}': {error}", + path.display() + ) + })? + .join(corpus.path); + + Ok(corpus) } /// Scan the corpus base directory and return all tests found.