Skip to content

Commit

Permalink
Fix potential segfault if lua GCs an s-expression source while it's s…
Browse files Browse the repository at this point in the history
…till being parsed
  • Loading branch information
bcrist committed Oct 22, 2022
1 parent 3a67e04 commit 6d935ba
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/lua-sexpr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn openSx(l: L) callconv(.C) c_int {
}
c.lua_pushvalue(l, -2);
c.lua_callk(l, 1, 0, 0, null);

var funcs = [_]c.luaL_Reg {
.{ .name = "parser", .func = sexprParser },
.{ .name = null, .func = null },
Expand Down Expand Up @@ -77,18 +77,26 @@ fn sexprParser(l: L) callconv(.C) c_int {
var parser = @ptrCast(*Parser, @alignCast(8, c.lua_newuserdata(l, @sizeOf(Parser))));
c.luaL_setmetatable(l, "class SxParser");

var alloc = allocators.global_gpa.allocator();

var ownedSource: []const u8 = alloc.dupe(u8, source) catch |e| {
_ = c.luaL_error(l, @errorName(e).ptr);
unreachable;
};

parser.* = .{
.stream = std.io.fixedBufferStream(source),
.stream = std.io.fixedBufferStream(ownedSource),
.reader = undefined,
};
parser.reader = sx.reader(allocators.global_gpa.allocator(), parser.stream.reader());
parser.reader = sx.reader(alloc, parser.stream.reader());

return 1;
}

fn parser__gc(l: L) callconv(.C) c_int {
var parser = @ptrCast(*Parser, @alignCast(8, c.luaL_checkudata(l, 1, "class SxParser")));
parser.reader.deinit();
allocators.global_gpa.allocator().free(parser.stream.buffer);
return 0;
}

Expand Down

0 comments on commit 6d935ba

Please sign in to comment.