During advent of code I wrote this function:
fn groupPebbles(groups: *std.ArrayList(PebbleGroup)) !void {
std.mem.sortUnstable(PebbleGroup, groups.items, {}, PebbleGroup.lessThan);
const e = groups.items[0];
try groups.append(e);
...
}
This worked fine at the time of writing it. About a week later, however, I updated Zig and it started segfaulting:
Segmentation fault at address 0x7f8124999000
/home/markus/.local/share/zigup/0.14.0-dev.2568+42dac40b3/files/lib/std/array_list.zig:263:13: 0x103d6e9 in append (11)
new_item_ptr.* = item;
^
/home/markus/Development/aoc2024/src/11/main.zig:73:22: 0x103d3bf in groupPebbles (11)
try groups.append(e);
^
I am pretty sure that this is not supposed to happen, and the workaround I found was to simply create a new pebble group manually from the old one (.{ .a = a, .b = b, ... }
-style).
I mean it’s fairly obvious what is happening here: The compiler is optimizing const e
away, which already existed only to avoid this issue before the compiler update. This leads to the code only accessing e
(aka items[0]
when the array may have been resized, which leads to a segfault.
Is this known behaveior?