Here’s a little demonstration of the bug.
this is 0.15.2
const std = @import("std");
const Vector2 = struct {
x: f32,
y: f32,
};
const Tile = struct {
id: usize,
position: Vector2,
rotation: f32 = 0,
};
pub fn main() !void {
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
defer _ = debug_allocator.deinit();
const gpa = debug_allocator.allocator();
var buffer: [2048]u8 = undefined;
var alloc_w: std.Io.Writer.Allocating = .init(gpa);
defer alloc_w.deinit();
var file = try std.fs.cwd().openFile("./map.zon", .{});
var file_reader = file.reader(&buffer);
_ = try file_reader.interface.streamRemaining(&alloc_w.writer);
var diag: std.zon.parse.Diagnostics = .{};
defer diag.deinit(gpa);
const res = std.zon.parse.fromSlice([]Tile, gpa, @ptrCast(alloc_w.written()), &diag, .{}) catch |err| {
std.debug.print("######################\n", .{});
std.debug.print("{f}", .{diag});
std.debug.print("######################\n", .{});
return err;
};
std.debug.print("{any}\n", .{res});
}
When I try to parse this .zon file for example.
.{ .{
.id = 24,
.position = .{ .x = 256, .y = 256 },
.rotation = 0,
}, .{
.id = 24,
.position = .{ .x = 576, .y = 192 },
.rotation = 0,
} }
I get this index of out bounds error.
thread 221360 panic: index out of bounds: index 165, len 164
/home/karim/.local/share/mise/installs/zig/0.15.2/lib/std/zig/tokenizer.zig:526:36: 0x11734f6 in next (std.zig)
switch (self.buffer[self.index]) {
^
/home/karim/.local/share/mise/installs/zig/0.15.2/lib/std/zig/Ast.zig:155:37: 0x1167ec9 in parse (std.zig)
const token = tokenizer.next();
^
/home/karim/.local/share/mise/installs/zig/0.15.2/lib/std/zon/parse.zig:278:36: 0x1160a18 in fromSlice__anon_22882 (std.zig)
var ast = try std.zig.Ast.parse(gpa, source, .zon);
^
/home/karim/Documents/projects/code/zig-test/main.zig:32:40: 0x115eab1 in main (main.zig)
const res = std.zon.parse.fromSlice([]Tile, gpa, @ptrCast(alloc_w.written()), &diag, .{}) catch |err| {
^
/home/karim/.local/share/mise/installs/zig/0.15.2/lib/std/start.zig:627:37: 0x115f685 in posixCallMainAndExit (std.zig)
const result = root.main() catch |err| {
^
/home/karim/.local/share/mise/installs/zig/0.15.2/lib/std/start.zig:232:5: 0x115e6b1 in _start (std.zig)
asm volatile (switch (native_arch) {
^
???:?:?: 0x0 in ??? (???)
zsh: IOT instruction (core dumped) zig run main.zig
I’m honestly not sure if this a bug in the parser or some mistake i made…