Commit 4d31e3c917

Mateusz Poliwczak <mpoliwczak34@gmail.com>
2023-03-23 18:01:21
std.base64: don't overflow dest with padding
1 parent 38ee46d
Changed files (1)
lib
lib/std/base64.zig
@@ -117,7 +117,7 @@ pub const Base64Encoder = struct {
             out_idx += 1;
         }
         if (encoder.pad_char) |pad_char| {
-            for (dest[out_idx..]) |*pad| {
+            for (dest[out_idx..out_len]) |*pad| {
                 pad.* = pad_char;
             }
         }
@@ -305,6 +305,20 @@ test "base64" {
     comptime try testAllApis(standard, "comptime", "Y29tcHRpbWU=");
 }
 
+test "base64 padding dest overflow" {
+    const input = "foo";
+
+    var expect: [128]u8 = undefined;
+    std.mem.set(u8, &expect, 0);
+    _ = url_safe.Encoder.encode(expect[0..url_safe.Encoder.calcSize(input.len)], input);
+
+    var got: [128]u8 = undefined;
+    std.mem.set(u8, &got, 0);
+    _ = url_safe.Encoder.encode(&got, input);
+
+    try std.testing.expectEqualSlices(u8, &expect, &got);
+}
+
 test "base64 url_safe_no_pad" {
     @setEvalBranchQuota(8000);
     try testBase64UrlSafeNoPad();