Skip to content

Commit

Permalink
base64ct: reject zero-length decode requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalmer committed Apr 24, 2024
1 parent 1cb066a commit e72445c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion base64ct/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ impl<'i, E: Encoding> Decoder<'i, E> {
///
/// # Returns
/// - `Ok(bytes)` if the expected amount of data was read
/// - `Err(Error::InvalidLength)` if the exact amount of data couldn't be read
/// - `Err(Error::InvalidLength)` if the exact amount of data couldn't be read, or
/// if the output buffer has a length of 0
pub fn decode<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8], Error> {
if out.is_empty() {
return Err(InvalidLength);
}

if self.is_finished() {
return Err(InvalidLength);
}
Expand Down Expand Up @@ -547,6 +552,8 @@ impl<'i> Iterator for LineReader<'i> {
mod tests {
use crate::{alphabet::Alphabet, test_vectors::*, Base64, Base64Unpadded, Decoder};

#[cfg(feature = "std")]
use crate::Error::InvalidLength;
#[cfg(feature = "std")]
use {alloc::vec::Vec, std::io::Read};

Expand Down Expand Up @@ -592,6 +599,16 @@ mod tests {
assert_eq!(buf.as_slice(), MULTILINE_PADDED_BIN);
}

#[cfg(feature = "std")]
#[test]
fn reject_empty_read() {
let mut decoder = Decoder::<Base64>::new(b"AAAA").unwrap();

let mut buf: Vec<u8> = vec![];

assert_eq!(decoder.decode(&mut buf), Err(InvalidLength));
}

/// Core functionality of a decoding test
#[allow(clippy::arithmetic_side_effects)]
fn decode_test<'a, F, V>(expected: &[u8], f: F)
Expand Down

0 comments on commit e72445c

Please sign in to comment.