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...@@ -1507,7 +1507,7 @@ pub fn joinZ(allocator: *Allocator, separator: []const u8, slices: []const []con
1507}1507}
15081508
1509fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []const u8, zero: bool) ![]u8 {1509fn 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
1512 const total_len = blk: {1512 const total_len = blk: {
1513 var sum: usize = separator.len * (slices.len - 1);1513 var sum: usize = separator.len * (slices.len - 1);
...@@ -1535,6 +1535,11 @@ fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []co...@@ -1535,6 +1535,11 @@ fn joinMaybeZ(allocator: *Allocator, separator: []const u8, slices: []const []co
1535}1535}
15361536
1537test "mem.join" {1537test "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 }
1538 {1543 {
1539 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });1544 const str = try join(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });
1540 defer testing.allocator.free(str);1545 defer testing.allocator.free(str);
...@@ -1553,6 +1558,12 @@ test "mem.join" {...@@ -1553,6 +1558,12 @@ test "mem.join" {
1553}1558}
15541559
1555test "mem.joinZ" {1560test "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 }
1556 {1567 {
1557 const str = try joinZ(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });1568 const str = try joinZ(testing.allocator, ",", &[_][]const u8{ "a", "b", "c" });
1558 defer testing.allocator.free(str);1569 defer testing.allocator.free(str);