You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When we create an std.ArrayList of a struct, if we try to append a new value into the arraylist, it raises a segmentation fault when the capacity is being expanded.
conststd=@import("std");
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
constBasket=struct {
x: f32=0.0,
y: f32=0.0,
z: f32=0.0,
};
pubfnmain() anyerror!void {
varbasket_list=std.ArrayList(Basket).init(&gpa.allocator);
basket_list.append(.{}) catchunreachable;
vari: usize=0;
while (i<20) : (i+=1) {
std.debug.print("i = {d}\n", .{i});
// This line fails at i = 7basket_list.append(basket_list.items[0]) catchunreachable;
// This always succeeds // var first = basket_list.items[0];// basket_list.append(first) catch unreachable;
}
}
Segmentation fault at address 0x1dcfd5e0000
C:\Users\user\zig-windows-x86_64-0.8.0-dev.1983+e2cc02717\lib\zig\std\array_list.zig:183:30: 0x7ff70d8ca85f in std.array_list.ArrayListAligned(Basket,null)::std.array_list.ArrayListAligned(Basket,null).append (error1.obj)
new_item_ptr.* = item;
^
C:\Users\user\zig_playground\error1\src\main.zig:17:27: 0x7ff70d8b407b in main (error1.obj)
basket_list.append(basket_list.items[0]) catch unreachable;
^
thread 3688 panic: integer overflow
C:\Users\user\zig-windows-x86_64-0.8.0-dev.1983+e2cc02717\lib\zig\std\debug.zig:139:69: 0x7ff70d8d2426 in std.debug.dumpStackTraceFromBase (error1.obj)
printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return;
^
C:\Users\user\zig-windows-x86_64-0.8.0-dev.1983+e2cc02717\lib\zig\std\debug.zig:1876:31: 0x7ff70d8ca5ce in std.debug.handleSegfaultWindowsExtra (error1.obj)
dumpStackTraceFromBase(regs.bp, regs.ip);
^
C:\Users\user\zig-windows-x86_64-0.8.0-dev.1983+e2cc02717\lib\zig\std\debug.zig:1853:73: 0x7ff70d8b3ee8 in std.debug.handleSegfaultWindows (error1.obj)
windows.EXCEPTION_ACCESS_VIOLATION => handleSegfaultWindowsExtra(info, 1, null),
I am not sure if this is the assumed behaviour. From what I understand, we should be passing the value of the item at index 0, and not a pointer, whereas the error makes it appear like we are passing a pointer.
This only happens when the ArrayList holds a struct. With other types like f32, the issue does not seem to arise.
I am using version 0.8.0-dev.1983+e2cc0271 on Windows.
The text was updated successfully, but these errors were encountered:
This is another bug caused by the implicit value-to-pointer conversion of struct parameters. The first parameter to append is transformed to a pointer with &basket_list.items[0]. Within the function, the array is expanded and the old array is deallocated. This pointer now points to invalid memory. Then the code attempts to copy from this pointer into the new array, causing the segfault.
When we create an
std.ArrayList
of astruct
, if we try to append a new value into the arraylist, it raises a segmentation fault when the capacity is being expanded.I am not sure if this is the assumed behaviour. From what I understand, we should be passing the value of the item at index 0, and not a pointer, whereas the error makes it appear like we are passing a pointer.
This only happens when the
ArrayList
holds astruct
. With other types likef32
, the issue does not seem to arise.I am using version 0.8.0-dev.1983+e2cc0271 on Windows.
The text was updated successfully, but these errors were encountered: