Skip to content

Commit 33f0eba

Browse files
committed
der: track reader error position in Uint::decode_value
1 parent 19aa61b commit 33f0eba

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

der/src/asn1/integer/uint.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
AnyRef, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader,
66
Result, Tag, ValueOrd, Writer, ord::OrdIsValueOrd,
77
};
8-
use core::cmp::Ordering;
8+
use core::{cmp::Ordering, result};
99

1010
#[cfg(feature = "alloc")]
1111
pub use allocating::Uint;
@@ -33,7 +33,8 @@ macro_rules! impl_encoding_traits {
3333
}
3434

3535
let bytes = reader.read_into(&mut buf[..max_length])?;
36-
let result = Self::from_be_bytes(decode_to_array(bytes)?);
36+
let bytes = decode_to_array(bytes).map_err(|kind| reader.error(kind))?;
37+
let result = Self::from_be_bytes(bytes);
3738

3839
// Ensure we compute the same encoded length as the original any value
3940
if header.length() != result.value_len()? {
@@ -123,7 +124,7 @@ impl<'a> DecodeValue<'a> for UintRef<'a> {
123124

124125
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
125126
let bytes = <&'a BytesRef>::decode_value(reader, header)?.as_slice();
126-
let result = Self::new(decode_to_slice(bytes)?)?;
127+
let result = Self::new(decode_to_slice(bytes).map_err(|kind| reader.error(kind))?)?;
127128

128129
// Ensure we compute the same encoded length as the original any value.
129130
if result.value_len()? != header.length() {
@@ -218,7 +219,8 @@ mod allocating {
218219

219220
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
220221
let bytes = BytesOwned::decode_value_parts(reader, header, Self::TAG)?;
221-
let result = Self::new(decode_to_slice(bytes.as_slice())?)?;
222+
let result =
223+
Self::new(decode_to_slice(bytes.as_slice()).map_err(|kind| reader.error(kind))?)?;
222224

223225
// Ensure we compute the same encoded length as the original any value.
224226
if result.value_len()? != header.length() {
@@ -319,7 +321,7 @@ mod allocating {
319321
/// zeroes removed.
320322
///
321323
/// Returns a byte array of the requested size containing a big endian integer.
322-
pub(crate) fn decode_to_slice(bytes: &[u8]) -> Result<&[u8]> {
324+
pub(crate) fn decode_to_slice(bytes: &[u8]) -> result::Result<&[u8], ErrorKind> {
323325
// The `INTEGER` type always encodes a signed value, so for unsigned
324326
// values the leading `0x00` byte may need to be removed.
325327
//
@@ -338,7 +340,7 @@ pub(crate) fn decode_to_slice(bytes: &[u8]) -> Result<&[u8]> {
338340

339341
/// Decode an unsigned integer into a byte array of the requested size
340342
/// containing a big endian integer.
341-
pub(super) fn decode_to_array<const N: usize>(bytes: &[u8]) -> Result<[u8; N]> {
343+
pub(super) fn decode_to_array<const N: usize>(bytes: &[u8]) -> result::Result<[u8; N], ErrorKind> {
342344
let input = decode_to_slice(bytes)?;
343345

344346
// Compute number of leading zeroes to add
@@ -412,20 +414,20 @@ mod tests {
412414
#[test]
413415
fn decode_to_array_extra_zero() {
414416
let err = decode_to_array::<4>(&[0, 1, 2]).err().unwrap();
415-
assert_eq!(err.kind(), ErrorKind::Noncanonical { tag: Tag::Integer });
417+
assert_eq!(err, ErrorKind::Noncanonical { tag: Tag::Integer });
416418
}
417419

418420
#[test]
419421
fn decode_to_array_missing_zero() {
420422
// We're decoding an unsigned integer, but this value would be signed
421423
let err = decode_to_array::<4>(&[0xFF, 0xFE]).err().unwrap();
422-
assert_eq!(err.kind(), ErrorKind::Value { tag: Tag::Integer });
424+
assert_eq!(err, ErrorKind::Value { tag: Tag::Integer });
423425
}
424426

425427
#[test]
426428
fn decode_to_array_oversized_input() {
427429
let err = decode_to_array::<1>(&[1, 2, 3]).err().unwrap();
428-
assert_eq!(err.kind(), ErrorKind::Length { tag: Tag::Integer });
430+
assert_eq!(err, ErrorKind::Length { tag: Tag::Integer });
429431
}
430432

431433
#[test]

0 commit comments

Comments
 (0)