mirror of
https://github.com/ziglang/zig.git
synced 2025-12-26 16:13:07 +00:00
Fix interger overflow when calling joinZ with empty slices
This commit is contained in:
parent
1ed8c54cd3
commit
236db6232f
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user