From 2cb4cc80ef73d846a53c636acb2240fd968f9620 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 25 Oct 2026 11:02:32 -0600 Subject: [PATCH] der: fix recursion in `TryFrom>` impl for `bool` --- der/src/asn1/boolean.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/der/src/asn1/boolean.rs b/der/src/asn1/boolean.rs index e5be13813..0bb48a91c 100644 --- a/der/src/asn1/boolean.rs +++ b/der/src/asn1/boolean.rs @@ -50,14 +50,35 @@ impl TryFrom> for bool { type Error = Error; fn try_from(any: AnyRef<'_>) -> Result { - any.try_into() + any.decode_as() } } #[cfg(test)] #[allow(clippy::unwrap_used, clippy::bool_assert_comparison)] mod tests { - use crate::{Decode, Encode}; + use crate::{Decode, Encode, asn1::AnyRef}; + + #[test] + fn try_from_any() { + let true_value = AnyRef::from_der(&[0x01, 0x01, 0xFF]).unwrap(); + let false_value = AnyRef::from_der(&[0x01, 0x01, 0x00]).unwrap(); + assert_eq!(bool::try_from(true_value).unwrap(), true); + assert_eq!(bool::try_from(false_value).unwrap(), false); + } + + #[test] + fn try_from_any_rejects_invalid_input() { + for bytes in [ + &[0x02, 0x01, 0x00][..], // Wrong tag: INTEGER. + &[0x01, 0x00], // Empty BOOLEAN. + &[0x01, 0x02, 0x00, 0x00], // Overlong BOOLEAN. + &[0x01, 0x01, 0x01], // Noncanonical TRUE. + ] { + let any = AnyRef::from_der(bytes).unwrap(); + assert!(bool::try_from(any).is_err()); + } + } #[test] fn decode() {