diff --git a/der/src/asn1/real.rs b/der/src/asn1/real.rs index f69ec96a6..d0fb3c587 100644 --- a/der/src/asn1/real.rs +++ b/der/src/asn1/real.rs @@ -1,4 +1,5 @@ -//! ASN.1 `NULL` support. +//! ASN.1 `REAL` support. + use crate::{ str_slice::StrSlice, ByteSlice, DecodeValue, Decoder, EncodeValue, Encoder, FixedTag, Header, Length, Result, Tag, @@ -6,6 +7,8 @@ use crate::{ use super::integer::uint::strip_leading_zeroes; +// TODO: panic-free implementation +#[allow(clippy::panic_in_result_fn)] impl DecodeValue<'_> for f64 { fn decode_value(decoder: &mut Decoder<'_>, header: Header) -> Result { let bytes = ByteSlice::decode_value(decoder, header)?.as_bytes(); @@ -15,8 +18,10 @@ impl DecodeValue<'_> for f64 { } else if is_nth_bit_one::<7>(bytes) { // Binary encoding from section 8.5.7 applies let sign: u64 = if is_nth_bit_one::<6>(bytes) { 1 } else { 0 }; + // Section 8.5.7.2: Check the base -- the DER specs say that only base 2 should be supported in DER let base = mnth_bits_to_u8::<5, 4>(bytes); + if base != 0 { // Real related error: base is not DER compliant (base encoded in enum) return Err(Tag::Real.value_error()); @@ -60,7 +65,9 @@ impl DecodeValue<'_> for f64 { 1 => Ok(f64::NEG_INFINITY), 2 => Ok(f64::NAN), 3 => Ok(-0.0_f64), - _ => unreachable!(), + _ => { + unreachable!() + } } } else { let astr = StrSlice::from_bytes(&bytes[1..])?; @@ -158,7 +165,10 @@ impl EncodeValue for f64 { 0 | 1 => {} 2 => first_byte |= 0b0000_0001, 3 => first_byte |= 0b0000_0010, - _ => todo!("support multi octet exponent encoding"), + _ => { + // TODO: support multi octet exponent encoding? + return Err(Tag::Real.value_error()); + } } encoder.bytes(&[first_byte])?; diff --git a/der/src/lib.rs b/der/src/lib.rs index 12ac3877e..16dc8ce78 100644 --- a/der/src/lib.rs +++ b/der/src/lib.rs @@ -8,6 +8,8 @@ )] #![forbid(unsafe_code, clippy::unwrap_used)] #![warn( + clippy::panic, + clippy::panic_in_result_fn, missing_docs, rust_2018_idioms, unused_lifetimes, @@ -25,6 +27,7 @@ //! - [`bool`]: ASN.1 `BOOLEAN`. //! - [`i8`], [`i16`], [`i32`], [`i64`], [`i128`]: ASN.1 `INTEGER`. //! - [`u8`], [`u16`], [`u32`], [`u64`], [`u128`]: ASN.1 `INTEGER`. +//! - [`f64`]: ASN.1 `REAL` //! - [`str`], [`String`][`alloc::string::String`]: ASN.1 `UTF8String`. //! `String` requires `alloc` feature. See also [`Utf8String`]. //! Requires `alloc` feature. See also [`SetOf`].