authorgravatar for fancl20@gmail.comroot <fancl20@gmail.com> 2021-01-26 22:40:34+11:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-01-27 12:01:18+02:00
log236db6232fa2beb97c47e3f1edcdb2a29e59d160
tree4c250dc068bc17efcb6ed524869894b93bfb58e3
parent1ed8c54cd349497adb264b0502783a6422e4f2d1

Fix interger overflow when calling joinZ with empty slices


1 files changed, 12 insertions(+), 1 deletions(-)

lib/std/mem.zig+12-1
......@@ -1507,7 +1507,7 @@ pub fn joinZ(allocator: *Allocator, separator: []const u8, slices: []const []con
15071507}
15081508
15091509fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []const u8, zero: bool) ![]u8 {
1510 if (slices.len == 0) return &[0]u8{};
1510 if (slices.len == 0) return if (zero) try allocator.dupe(u8, &[1]u8{0}) else &[0]u8{};
15111511
15121512 const total_len = blk: {
15131513 var sum: usize = separator.len * (slices.len - 1);
......@@ -1535,6 +1535,11 @@ fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []co
15351535}
15361536
15371537test "mem.join" {
1538 {
1539 const str = try join(testing.allocator, ",", &[_][]const u8{});
1540 defer testing.allocator.free(str);
1541 testing.expect(eql(u8, str, ""));
1542 }
15381543 {
15391544 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });
15401545 defer testing.allocator.free(str);
......@@ -1553,6 +1558,12 @@ test "mem.join" {
15531558}
15541559
15551560test "mem.joinZ" {
1561 {
1562 const str = try joinZ(testing.allocator, ",", &[_][]const u8{});
1563 defer testing.allocator.free(str);
1564 testing.expect(eql(u8, str, ""));
1565 testing.expectEqual(str[str.len], 0);
1566 }
15561567 {
15571568 const str = try joinZ(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });
15581569 defer testing.allocator.free(str);