zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 236db6232fa2beb97c47e3f1edcdb2a29e59d160 (tree)
parent 1ed8c54cd349497adb264b0502783a6422e4f2d1
Author: root <fancl20@gmail.com>
Date:   Tue, 26 Jan 2021 22:40:34 +1100

Fix interger overflow when calling joinZ with empty slices

Diffstat:
Mlib/std/mem.zig | 13++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)

diff --git 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); @@ -1536,6 +1536,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); testing.expect(eql(u8, str, "a,b,c")); @@ -1554,6 +1559,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); testing.expect(eql(u8, str, "a,b,c"));