commit 69f46cab555edd3fda9012a237aa7f708c144bfe (tree)
parent 452c35656ed51944e164419e545381e889b4deb2
Author: Jean Dao <jean@pfudke.fr>
Date: Fri, 28 Jan 2022 10:40:03 +0100
fix argsAlloc buffer size
The buffer `buf` contains N (= `slice_sizes.len`) slices followed by the
N null-terminated arguments. The N null-terminated arguments are stored
in the `contents` array list. Thus, `buf` size should be:
@sizeOf([]u8) * slice_sizes.len + contents_slice.len
Instead of:
@sizeOf([]u8) * slice_sizes.len + contents_slice.len + slice_sizes.len
This bug was found thanks to the gpa allocator which checks if freed
size matches allocated sizes for large allocations.
Diffstat:
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/lib/std/process.zig b/lib/std/process.zig
@@ -559,9 +559,8 @@ pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 {
const contents_slice = contents.items;
const slice_sizes = slice_list.items;
- const contents_size_bytes = try math.add(usize, contents_slice.len, slice_sizes.len);
const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len);
- const total_bytes = try math.add(usize, slice_list_bytes, contents_size_bytes);
+ const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len);
const buf = try allocator.alignedAlloc(u8, @alignOf([]u8), total_bytes);
errdefer allocator.free(buf);