Skip to content

Commit ade0ecb

Browse files
committed
base64ct: reject zero-length decode requests
1 parent 1cb066a commit ade0ecb

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

base64ct/src/decoder.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ impl<'i, E: Encoding> Decoder<'i, E> {
103103
///
104104
/// # Returns
105105
/// - `Ok(bytes)` if the expected amount of data was read
106-
/// - `Err(Error::InvalidLength)` if the exact amount of data couldn't be read
106+
/// - `Err(Error::InvalidLength)` if the exact amount of data couldn't be read, or
107+
/// if the output buffer has a length of 0
107108
pub fn decode<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8], Error> {
109+
if out.is_empty() {
110+
return Err(InvalidLength);
111+
}
112+
108113
if self.is_finished() {
109114
return Err(InvalidLength);
110115
}
@@ -592,6 +597,16 @@ mod tests {
592597
assert_eq!(buf.as_slice(), MULTILINE_PADDED_BIN);
593598
}
594599

600+
#[cfg(feature = "std")]
601+
#[test]
602+
fn reject_empty_read() {
603+
let mut decoder = Decoder::<Base64>::new(b"AAAA").unwrap();
604+
605+
let mut buf: Vec<u8> = vec![];
606+
607+
assert_eq!(decoder.decode(&mut buf), Err(InvalidLength));
608+
}
609+
595610
/// Core functionality of a decoding test
596611
#[allow(clippy::arithmetic_side_effects)]
597612
fn decode_test<'a, F, V>(expected: &[u8], f: F)

0 commit comments

Comments
 (0)