diff --git a/src/lib.rs b/src/lib.rs index 5a3051ac..e6e76c0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -605,6 +605,27 @@ impl<'de> Deserializer<'de> { /// architecture dependant `find_structural_bits` impl Deserializer<'_> { + #[cfg_attr(not(feature = "no-inline"), inline)] + /// Native fallback that pre-validates UTF-8 before finding structural bits, + /// since the native `ChunkedUtf8ValidatorImp` is a no-op. + #[cfg(all( + feature = "runtime-detection", + any(target_arch = "x86_64", target_arch = "x86"), + not(feature = "portable"), + ))] + pub(crate) unsafe fn find_structural_bits_native( + input: &[u8], + structural_indexes: &mut Vec, + ) -> std::result::Result<(), ErrorType> { + match core::str::from_utf8(input) { + Ok(_) => (), + Err(_) => return Err(ErrorType::InvalidUtf8), + }; + unsafe { + Self::_find_structural_bits::(input, structural_indexes) + } + } + #[cfg_attr(not(feature = "no-inline"), inline)] #[cfg(all( feature = "runtime-detection", @@ -629,7 +650,7 @@ impl Deserializer<'_> { #[cfg(feature = "portable")] let r = Deserializer::_find_structural_bits::; #[cfg(not(feature = "portable"))] - let r = Deserializer::_find_structural_bits::; + let r = Deserializer::find_structural_bits_native; r } }