From a8c3ab1915c9dc9cecf4ae93b5337d65f1bfffbf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 21 Aug 2026 12:45:35 -0700 Subject: [PATCH] simd: fix scalar base64 empty input handling causing a crash --- src/simd/base64.zig | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/simd/base64.zig b/src/simd/base64.zig index 81feeb723..a0ba45ea9 100644 --- a/src/simd/base64.zig +++ b/src/simd/base64.zig @@ -53,9 +53,9 @@ fn decodeScalar( /// For non-SIMD enabled builds, we trim the padding from the end of the /// base64 input in order to get identical output with the SIMD version. fn scalarInput(input: []const u8) []const u8 { - var i: usize = 0; - while (input[input.len - i - 1] == '=') i += 1; - return input[0 .. input.len - i]; + var end = input.len; + while (end > 0 and input[end - 1] == '=') end -= 1; + return input[0..end]; } // base64.cpp @@ -75,6 +75,14 @@ test "base64 maxLen" { try testing.expectEqual(11, len); } +test "base64 empty input" { + const testing = std.testing; + var output: [0]u8 = .{}; + + try testing.expectEqual(0, maxLen("")); + try testing.expectEqualStrings("", try decode("", &output)); +} + test "base64 decode" { const testing = std.testing; const alloc = testing.allocator;