commit 1cbc3d0453055fa75b15797e937f4abafe53bcbc
parent cbc5cdbde772de3a7321951eb14d5d7d0bb73317
Author: Thomas Bracht Laumann Jespersen <t@laumann.xyz>
Date: Tue, 27 Dec 2022 08:00:12 +0100
encoding/base64: fix partial write bug
Co-authored-by: Armin Preiml <apreiml@strohwolke.at>
Signed-off-by: Thomas Bracht Laumann Jespersen <t@laumann.xyz>
Diffstat:
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha
@@ -96,7 +96,7 @@ fn encode_writer(
static let b: [3]u8 = [0...]; // 3 bytes get converted into 4 bytes
if (i < s.avail) {
for (let j = 0z; j < s.avail; j += 1) {
- b[j] = s.buf[i];
+ b[j] = s.buf[j];
};
for (let j = s.avail; j < 3; j += 1) {
b[j] = in[j - s.avail];
@@ -160,6 +160,23 @@ fn encode_closer(s: *io::stream) (void | io::error) = {
io::writeall(s.out, encb)?;
};
+@test fn partialwrite() void = {
+ const raw: [_]u8 = [
+ 0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73,
+ 0x61, 0x00,
+ ];
+ const expected: str = `AAAAB3NzaC1yc2EA`;
+
+ let buf = bufio::dynamic(io::mode::WRITE);
+ let e = newencoder(&std_encoding, &buf);
+ io::writeall(&e, raw[..4])!;
+ io::writeall(&e, raw[4..11])!;
+ io::writeall(&e, raw[11..])!;
+ io::close(&e)!;
+
+ assert(strings::fromutf8(bufio::buffer(&buf))! == expected);
+};
+
// Encodes a byte slice in base 64, using the given encoding, returning a slice
// of ASCII bytes. The caller must free the return value.
export fn encodeslice(enc: *encoding, in: []u8) []u8 = {