Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions ludicrousdb/includes/class-ludicrousdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ protected function single_db_connect( $dbhname, $host, $user, $password ) {
$port_or_socket = substr( $port_or_socket, 1 );

if ( 0 !== strpos( $port_or_socket, '/' ) ) {
$port = intval( $port_or_socket );
$port = (int) $port_or_socket;
$maybe_socket = strstr( $port_or_socket, ':' );

if ( ! empty( $maybe_socket ) ) {
Expand Down Expand Up @@ -2311,9 +2311,39 @@ public function log_query( $query = '', $query_time = 0, $query_callstack = '',
* @param int $port Port or socket.
* @param float $float_timeout Timeout in seconds, as float number ().
*
* @return bool true when $host:$post responds within $float_timeout seconds, else false
* @return bool true when $host:$port responds within $float_timeout seconds, else false
*/
public function check_tcp_responsiveness( $host, $port, $float_timeout ) {
$socket = '';

// Bail if disabled
if ( empty( $this->check_tcp_responsiveness ) ) {
$this->tcp_responsive = true;
return true;
}

// Maybe split host:port:socket into $host and $port and $socket
if ( strpos( $host, ':' ) ) {
$port_or_socket = strstr( $host, ':' );

$host = substr( $host, 0, strpos( $host, ':' ) );
$port_or_socket = substr( $port_or_socket, 1 );

if ( 0 !== strpos( $port_or_socket, '/' ) ) {
$port = (int) $port_or_socket;
$maybe_socket = strstr( $port_or_socket, ':' );

if ( ! empty( $maybe_socket ) ) {
$socket = substr( $maybe_socket, 1 );
}
} else {
$socket = $port_or_socket;
}
}
Comment on lines +2325 to +2342

Copilot AI Dec 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket parsing logic overwrites the $port parameter without checking if it was already set. When this method is called from line 1057, the $port parameter may have already been extracted or defaulted (lines 992 and 997), but this parsing will overwrite it if the host string contains a colon. This creates inconsistency with the earlier parsing logic and could lead to unexpected behavior. Consider either: (1) only parsing if $port is null/empty, or (2) documenting that the $port parameter is ignored when $host contains socket information.

Copilot uses AI. Check for mistakes.
Comment on lines +2325 to +2342

Copilot AI Dec 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This socket parsing logic is duplicated from the single_db_connect() method (lines 1290-1308). Consider extracting this into a shared private method to avoid code duplication and ensure consistent parsing behavior across the codebase. For example:

private function parse_host_port_socket( $host, $default_port = 0 ) {
    $port = $default_port;
    $socket = '';
    // ... parsing logic ...
    return array( $host, $port, $socket );
}

Copilot uses AI. Check for mistakes.

if ( ! empty( $socket ) ) {
$socket = 'unix://' . $socket;
}

// Get the cache key
$cache_key = $this->tcp_get_cache_key( $host, $port );
Expand All @@ -2332,24 +2362,25 @@ public function check_tcp_responsiveness( $host, $port, $float_timeout ) {
return false;
}

if ( empty( $this->check_tcp_responsiveness ) ) {
$this->tcp_responsive = true;
return true;
}

// Defaults
$errno = 0;
$errstr = '';

// Try to get a new socket
// phpcs:disable
$socket = $this->is_debug()
? fsockopen( $host, $port, $errno, $errstr, $float_timeout )
: @fsockopen( $host, $port, $errno, $errstr, $float_timeout );
if ( empty( $socket ) ) {
$check_socket = $this->is_debug()
? fsockopen( $host, $port, $errno, $errstr, $float_timeout )
: @fsockopen( $host, $port, $errno, $errstr, $float_timeout );
} else {
$check_socket = $this->is_debug()
? fsockopen( $socket, - 1, $errno, $errstr, $float_timeout )
: @fsockopen( $socket, - 1, $errno, $errstr, $float_timeout );
}
// phpcs:enable

// No socket
if ( false === $socket ) {
if ( false === $check_socket ) {
$this->tcp_cache_set( $cache_key, 'down' );
$this->tcp_responsive = false;

Expand All @@ -2358,7 +2389,7 @@ public function check_tcp_responsiveness( $host, $port, $float_timeout ) {

// Close the socket
// phpcs:ignore
fclose( $socket );
fclose( $check_socket );

// Using API
$this->tcp_cache_set( $cache_key, 'up' );
Expand Down