From 236db6232fa2beb97c47e3f1edcdb2a29e59d160 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 Jan 2021 22:40:34 +1100 Subject: [PATCH] Fix interger overflow when calling joinZ with empty slices --- lib/std/mem.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 98a37e3d2b..5f23a10401 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1507,7 +1507,7 @@ pub fn joinZ(allocator: *Allocator, separator: []const u8, slices: []const []con } fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []const u8, zero: bool) ![]u8 { - if (slices.len == 0) return &[0]u8{}; + if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{}; const total_len = blk: { var sum: usize = separator.len * (slices.len - 1); @@ -1535,6 +1535,11 @@ fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []co } test "mem.join" { + { + const str = try join(testing.allocator, ",", &[_][]const u8{}); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "")); + } { const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" }); defer testing.allocator.free(str); @@ -1553,6 +1558,12 @@ test "mem.join" { } test "mem.joinZ" { + { + const str = try joinZ(testing.allocator, ",", &[_][]const u8{}); + defer testing.allocator.free(str); + testing.expect(eql(u8, str, "")); + testing.expectEqual(str[str.len], 0); + } { const str = try joinZ(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" }); defer testing.allocator.free(str);