diff --git a/aquatic_common/src/lib.rs b/aquatic_common/src/lib.rs index b419138..5034ce2 100644 --- a/aquatic_common/src/lib.rs +++ b/aquatic_common/src/lib.rs @@ -36,23 +36,24 @@ pub struct AccessList(HashSet<[u8; 20]>); impl AccessList { fn parse_line_to_info_hash(line: String) -> anyhow::Result<[u8; 20]> { - let mut count = 0usize; + if line.len() != 20 { + return Err(anyhow::anyhow!("Info hash is not 20 bytes long: {}", line)); + } + let mut bytes = [0u8; 20]; for (byte, c) in bytes.iter_mut().zip(line.chars()) { + if c as u32 > 255 { + return Err(anyhow::anyhow!( + "Info hash character is not ASCII: {:#?}", + c + )); + } + *byte = c as u8; - count += 1; } - if count == 20 { - Ok(bytes) - } else { - Err(anyhow::anyhow!( - "Info hash length only {} bytes: {}", - count, - line - )) - } + Ok(bytes) } pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> {