From 3b40e3168dde1dafcf42b038f32cd7474f0966fb Mon Sep 17 00:00:00 2001 From: Adam Harvey Date: Wed, 23 Aug 2023 14:29:02 -0700 Subject: [PATCH] Remove a couple of possible panics when opening a post. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm not actually so bothered about the panics per se, but this way a naïve user like myself who accidentally created a zero length file in `posts` gets a useful error message. --- src/posts.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/posts.rs b/src/posts.rs index f8a3a07fc..66298a49e 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -50,10 +50,15 @@ impl Post { let filename = split.next().unwrap().to_string(); let contents = std::fs::read_to_string(path)?; + if contents.len() < 5 { + return Err( + format!("{path:?} is empty, or too short to have valid front matter").into(), + ); + } // yaml headers.... we know the first four bytes of each file are "---\n" // so we need to find the end. we need the fours to adjust for those first bytes - let end_of_yaml = contents[4..].find("---").unwrap() + 4; + let end_of_yaml = contents[4..].find("---\n").unwrap() + 4; let yaml = &contents[..end_of_yaml]; let YamlHeader { author,