diff --git a/src/decoder/sorted_set.rs b/src/decoder/sorted_set.rs index a02e045..a9561a5 100644 --- a/src/decoder/sorted_set.rs +++ b/src/decoder/sorted_set.rs @@ -30,9 +30,16 @@ pub fn read_sorted_set( 255 => f64::NEG_INFINITY, _ => { let tmp = read_exact(input, score_length as usize)?; - unsafe { str::from_utf8_unchecked(&tmp) } + str::from_utf8(&tmp) + .map_err(|_| RdbError::ParsingError { + context: "read_sorted_set", + message: "Invalid UTF-8 in score".to_string(), + })? .parse::() - .unwrap() + .map_err(|_| RdbError::ParsingError { + context: "read_sorted_set", + message: "Invalid score format".to_string(), + })? } } }; @@ -105,7 +112,14 @@ pub fn read_sorted_set_listpack( let member = read_list_pack_entry_as_string(&mut reader)?; let score_str = read_list_pack_entry_as_string(&mut reader)?; - let score = unsafe { str::from_utf8_unchecked(&score_str) } + let score = str::from_utf8(&score_str) + .map_err(|_| RdbError::ParsingError { + context: "read_sorted_set_listpack", + message: format!( + "Failed to parse score: {:?}", + String::from_utf8_lossy(&score_str) + ), + })? .parse::() .map_err(|_| RdbError::ParsingError { context: "read_sorted_set_listpack", diff --git a/src/filter.rs b/src/filter.rs index ecf8dd5..e7b595d 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -61,8 +61,10 @@ impl Filter for Simple { match self.keys.clone() { None => true, Some(re) => { - let key = unsafe { str::from_utf8_unchecked(key) }; - re.is_match(key) + match str::from_utf8(key) { + Ok(key_str) => re.is_match(key_str), + Err(_) => false + } } } }