diff --git a/lib/std/base64.zig b/lib/std/base64.zig index 0b91a1cf0a..112410a32d 100644 --- a/lib/std/base64.zig +++ b/lib/std/base64.zig @@ -38,8 +38,8 @@ pub const Base64Encoder = struct { } /// dest.len must be what you get from ::calcSize. - pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) void { - assert(dest.len == Base64Encoder.calcSize(source.len)); + pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 { + assert(dest.len >= Base64Encoder.calcSize(source.len)); var i: usize = 0; var out_index: usize = 0; @@ -78,6 +78,7 @@ pub const Base64Encoder = struct { dest[out_index] = encoder.pad_char; out_index += 1; } + return dest[0..out_index]; } }; @@ -398,8 +399,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void // Base64Encoder { var buffer: [0x100]u8 = undefined; - var encoded = buffer[0..Base64Encoder.calcSize(expected_decoded.len)]; - standard_encoder.encode(encoded, expected_decoded); + const encoded = standard_encoder.encode(&buffer, expected_decoded); testing.expectEqualSlices(u8, expected_encoded, encoded); } diff --git a/lib/std/build/write_file.zig b/lib/std/build/write_file.zig index 923df960de..9bbe8830a3 100644 --- a/lib/std/build/write_file.zig +++ b/lib/std/build/write_file.zig @@ -72,7 +72,7 @@ pub const WriteFileStep = struct { var digest: [48]u8 = undefined; hash.final(&digest); var hash_basename: [64]u8 = undefined; - fs.base64_encoder.encode(&hash_basename, &digest); + _ = fs.base64_encoder.encode(&hash_basename, &digest); self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{ self.builder.cache_root, "o", diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 4c346a2c89..6880940c03 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -83,7 +83,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: tmp_path[dirname.len] = path.sep; while (true) { crypto.random.bytes(rand_buf[0..]); - base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf); + _ = base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf); if (cwd().symLink(existing_path, tmp_path, .{})) { return cwd().rename(tmp_path, new_path); @@ -153,15 +153,14 @@ pub const AtomicFile = struct { ) InitError!AtomicFile { var rand_buf: [RANDOM_BYTES]u8 = undefined; var tmp_path_buf: [TMP_PATH_LEN:0]u8 = undefined; - // TODO: should be able to use TMP_PATH_LEN here. - tmp_path_buf[base64.Base64Encoder.calcSize(RANDOM_BYTES)] = 0; while (true) { crypto.random.bytes(rand_buf[0..]); - base64_encoder.encode(&tmp_path_buf, &rand_buf); + const tmp_path = base64_encoder.encode(&tmp_path_buf, &rand_buf); + tmp_path_buf[tmp_path.len] = 0; const file = dir.createFile( - &tmp_path_buf, + tmp_path, .{ .mode = mode, .exclusive = true }, ) catch |err| switch (err) { error.PathAlreadyExists => continue, diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 63054892db..8e17c4e9f8 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -305,7 +305,7 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir { var random_bytes: [TmpDir.random_bytes_count]u8 = undefined; std.crypto.random.bytes(&random_bytes); var sub_path: [TmpDir.sub_path_len]u8 = undefined; - std.fs.base64_encoder.encode(&sub_path, &random_bytes); + _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes); var cwd = getCwdOrWasiPreopen(); var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch