Skip to content

Commit

Permalink
Fix crash in fs.readFile
Browse files Browse the repository at this point in the history
We were default-initializing the vm pointer to undefined
  • Loading branch information
Jarred-Sumner committed Jan 21, 2024
1 parent c00f078 commit 627d60c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/bun.js/api/bun.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1333,11 +1333,11 @@ pub fn getPublicPathJS(globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFr
}

fn fs(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSC.JSValue {
var module = globalObject.allocator().create(JSC.Node.NodeJSFS) catch unreachable;
module.* = .{};
const vm = globalObject.bunVM();
if (vm.standalone_module_graph != null)
module.node_fs.vm = vm;
var module = JSC.Node.NodeJSFS.new(.{
.node_fs = .{
.vm = globalObject.bunVM(),
},
});

return module.toJS(globalObject);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/node/node.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export default [
name: "NodeJSFS",
construct: true,
noConstructor: true,
finalize: false,
finalize: true,

klass: {},
proto: {
Expand Down
14 changes: 13 additions & 1 deletion src/bun.js/node/node_fs_binding.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const bun = @import("root").bun;
const JSC = @import("root").bun.JSC;
const std = @import("std");
const Flavor = JSC.Node.Flavor;
Expand Down Expand Up @@ -138,15 +139,26 @@ fn call(comptime FunctionEnum: NodeFSFunctionEnum) NodeFSFunction {
}

pub const NodeJSFS = struct {
node_fs: JSC.Node.NodeFS = undefined,
node_fs: JSC.Node.NodeFS = .{},

pub usingnamespace JSC.Codegen.JSNodeJSFS;
pub usingnamespace bun.New(@This());

pub fn constructor(globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) ?*@This() {
globalObject.throw("Not a constructor", .{});
return null;
}

pub fn finalize(this: *JSC.Node.NodeJSFS) callconv(.C) void {
if (this.node_fs.vm) |vm| {
if (vm.node_fs == this) {
return;
}
}

this.destroy();
}

pub const access = call(.access);
pub const appendFile = call(.appendFile);
pub const close = call(.close);
Expand Down

0 comments on commit 627d60c

Please sign in to comment.