diff options
author | 2023-01-05 23:17:15 +0200 | |
---|---|---|
committer | 2023-01-05 13:17:15 -0800 | |
commit | d22e3ebf9ab6fe2bdecd90fc4d65aa28bce6aae4 (patch) | |
tree | 1df4617794ca84e510fe095106f0da54b0a445da /src/bun.js/api | |
parent | 0873a15a637339506207fe9bd628e2839d78dbbc (diff) | |
download | bun-d22e3ebf9ab6fe2bdecd90fc4d65aa28bce6aae4.tar.gz bun-d22e3ebf9ab6fe2bdecd90fc4d65aa28bce6aae4.tar.zst bun-d22e3ebf9ab6fe2bdecd90fc4d65aa28bce6aae4.zip |
[socket] fix double-free in `finalize()` (#1731)
- tidy up `.isEmptyOrUndefinedOrNull()` usage
Diffstat (limited to 'src/bun.js/api')
-rw-r--r-- | src/bun.js/api/bun/socket.zig | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index bded03f7f..3f6ea883a 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -119,7 +119,7 @@ const Handlers = struct { } const result = onError.callWithThis(this.globalObject, thisValue, err); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(this.globalObject)) { + if (result.isAnyError(this.globalObject)) { this.vm.onUnhandledError(this.globalObject, result); } @@ -547,9 +547,7 @@ pub const Listener = struct { var listener = this.listener orelse return JSValue.jsUndefined(); this.listener = null; listener.close(this.ssl); - if (this.poll_ref.isActive()) { - this.poll_ref.unref(this.handlers.vm); - } + this.poll_ref.unref(this.handlers.vm); if (this.handlers.active_connections == 0) { this.handlers.unprotect(); this.socket_context.?.deinit(this.ssl); @@ -619,8 +617,6 @@ pub const Listener = struct { } pub fn unref(this: *Listener, globalObject: *JSC.JSGlobalObject, _: *JSC.CallFrame) callconv(.C) JSValue { - if (!this.poll_ref.isActive()) return JSValue.jsUndefined(); - this.poll_ref.unref(globalObject.bunVM()); if (this.handlers.active_connections == 0) { this.strong_self.clear(); @@ -857,7 +853,7 @@ fn NewSocket(comptime ssl: bool) type { this_value, }); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(globalObject)) { + if (result.isAnyError(globalObject)) { _ = handlers.callErrorHandler(this_value, &[_]JSC.JSValue{ this_value, result }); } } @@ -883,7 +879,7 @@ fn NewSocket(comptime ssl: bool) type { this_value, }); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(globalObject)) { + if (result.isAnyError(globalObject)) { _ = handlers.callErrorHandler(this_value, &[_]JSC.JSValue{ this_value, result }); } } @@ -967,7 +963,7 @@ fn NewSocket(comptime ssl: bool) type { this_value, }); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(globalObject)) { + if (result.isAnyError(globalObject)) { this.detached = true; defer this.markInactive(); if (!this.socket.isClosed()) { @@ -1010,7 +1006,7 @@ fn NewSocket(comptime ssl: bool) type { this_value, }); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(globalObject)) { + if (result.isAnyError(globalObject)) { _ = handlers.callErrorHandler(this_value, &[_]JSC.JSValue{ this_value, result }); } } @@ -1034,7 +1030,7 @@ fn NewSocket(comptime ssl: bool) type { JSValue.jsNumber(@as(i32, err)), }); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(globalObject)) { + if (result.isAnyError(globalObject)) { _ = handlers.callErrorHandler(this_value, &[_]JSC.JSValue{ this_value, result }); } } @@ -1057,7 +1053,7 @@ fn NewSocket(comptime ssl: bool) type { output_value, }); - if (!result.isEmptyOrUndefinedOrNull() and result.isAnyError(globalObject)) { + if (result.isAnyError(globalObject)) { _ = handlers.callErrorHandler(this_value, &[_]JSC.JSValue{ this_value, result }); } } @@ -1408,12 +1404,13 @@ fn NewSocket(comptime ssl: bool) type { pub fn finalize(this: *This) callconv(.C) void { log("finalize()", .{}); + if (this.detached) return; this.detached = true; if (!this.socket.isClosed()) { this.socket.close(0, null); } this.markInactive(); - if (this.poll_ref.isActive()) this.poll_ref.unref(JSC.VirtualMachine.get()); + this.poll_ref.unref(JSC.VirtualMachine.get()); } pub fn reload(this: *This, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue { |