harden core layer. panic on refcount underflow instead of silent error,
replace socket magic numbers with posix constants, fix race where socket state was set after closing FD
This commit is contained in:
@@ -432,10 +432,12 @@ pub const Connection = struct {
|
|||||||
// This was the last reference
|
// This was the last reference
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Safety check: underflow would indicate a bug
|
// Safety check: underflow is an invariant violation (double-free)
|
||||||
if (old_count == 0) {
|
if (old_count == 0) {
|
||||||
log.err("Connection reference count underflow! Double-free detected.", .{});
|
// fetchSub already wrapped the counter — this is unrecoverable corruption.
|
||||||
return Error.InternalError;
|
// Panic immediately so the bug is caught in development rather than
|
||||||
|
// silently propagated through the call chain.
|
||||||
|
@panic("Connection reference count underflow: double-free detected");
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -156,13 +156,13 @@ pub const Socket = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const domain: u32 = switch (self.config.protocol) {
|
const domain: u32 = switch (self.config.protocol) {
|
||||||
.tcp, .udp => 2, // AF_INET
|
.tcp, .udp => posix.AF.INET,
|
||||||
.unix_stream, .unix_dgram => 1, // AF_UNIX
|
.unix_stream, .unix_dgram => posix.AF.UNIX,
|
||||||
};
|
};
|
||||||
|
|
||||||
const sock_type: u32 = switch (self.config.protocol) {
|
const sock_type: u32 = switch (self.config.protocol) {
|
||||||
.tcp, .unix_stream => 1, // SOCK_STREAM
|
.tcp, .unix_stream => posix.SOCK.STREAM,
|
||||||
.udp, .unix_dgram => 2, // SOCK_DGRAM
|
.udp, .unix_dgram => posix.SOCK.DGRAM,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.fd = posix.socket(domain, sock_type, 0) catch |err| {
|
self.fd = posix.socket(domain, sock_type, 0) catch |err| {
|
||||||
@@ -192,9 +192,10 @@ pub const Socket = struct {
|
|||||||
log.err("CRITICAL: Failed to set non-blocking mode: {}", .{err});
|
log.err("CRITICAL: Failed to set non-blocking mode: {}", .{err});
|
||||||
self.stats.recordError();
|
self.stats.recordError();
|
||||||
// Close the socket since it's in an invalid state
|
// Close the socket since it's in an invalid state
|
||||||
|
// Set state BEFORE closing FD to prevent another thread from using a closed FD
|
||||||
|
self.state = .closed;
|
||||||
posix.close(self.fd);
|
posix.close(self.fd);
|
||||||
self.fd = -1;
|
self.fd = -1;
|
||||||
self.state = .closed;
|
|
||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user