Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail with InvalidLength when reading nothing at end of data #1387

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading