From 860d6e1b3a50d49b58b2853bf160420cd0b5eafe Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 16 Mar 2024 22:23:09 -0500 Subject: [PATCH] don't panic on invalid mutf8 --- simdnbt/src/mutf8.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/simdnbt/src/mutf8.rs b/simdnbt/src/mutf8.rs index ac8048c..ec3f582 100644 --- a/simdnbt/src/mutf8.rs +++ b/simdnbt/src/mutf8.rs @@ -104,9 +104,10 @@ impl Mutf8Str { // SAFETY: &[u8] and &str are the same layout. unsafe { Cow::Borrowed(std::str::from_utf8_unchecked(&self.slice)) } } else { - match mutf8::decode(&self.slice).expect("Mutf8Str must alwaus be valid MUTF-8") { - Cow::Borrowed(b) => Cow::Borrowed(b), - Cow::Owned(o) => Cow::Owned(o), + match mutf8::decode(&self.slice) { + Ok(Cow::Borrowed(b)) => Cow::Borrowed(b), + Ok(Cow::Owned(o)) => Cow::Owned(o), + Err(_) => Cow::Borrowed(""), } } } @@ -173,9 +174,10 @@ impl Mutf8String { // SAFETY: &[u8] and &str are the same layout. unsafe { String::from_utf8_unchecked(self.vec) } } else { - match mutf8::decode(&self.vec).expect("Mutf8Str must alwaus be valid MUTF-8") { - Cow::Borrowed(b) => b.to_owned(), - Cow::Owned(o) => o, + match mutf8::decode(&self.vec) { + Ok(Cow::Borrowed(b)) => b.to_owned(), + Ok(Cow::Owned(o)) => o, + Err(_) => String::new(), } } }