Skip to content

base64ct: allow exact-sized buffers for wrapped output - #2563

Open
mottopanikeiku wants to merge 1 commit into
RustCrypto:masterfrom
mottopanikeiku:base64ct-wrapped-exact-buffer
Open

mottopanikeiku wants to merge 1 commit into
RustCrypto:masterfrom
mottopanikeiku:base64ct-wrapped-exact-buffer

Conversation

@mottopanikeiku

Copy link
Copy Markdown

Summary

The wrapped encoder needed one spare byte when inserting a line ending before the last block, so an output buffer that fits the encoded text exactly failed with InvalidLength. I changed the capacity check to allow an exact fit and added a CRLF regression test.

Reproduce

use base64ct::{Base64, Encoder, LineEnding};

let mut buf = [0; 10];
let mut encoder = Encoder::<Base64>::new_wrapped(&mut buf, 4, LineEnding::CRLF).unwrap();
encoder.encode(b"abcdef").unwrap();
assert_eq!(encoder.finish().unwrap(), "YWJj\r\nZGVm");

Before, encode returned InvalidLength. Now it writes the ten bytes.

Tests

wrapped_output_fits_exact_buffer fails on master and passes here, along with the rest of cargo test -p base64ct --lib encoder::tests. fmt and clippy are clean.

I used AI assistance (Claude and GPT-6 Astra) to find, fix and test this.

I changed the newline insertion check to allow the last output byte to land at the end of the buffer. I added a two-line example that used to fail with InvalidLength.

Assisted-by: Claude
Assisted-by: GPT-6 Astra
Signed-off-by: mottopanikeiku <176798723+mottopanikeiku@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant