4242#include <ctype.h>
4343#include <stdatomic.h>
4444#include "intset.h"
45- #include <stdbool.h>
4645
47- /**
48- * This struct is used to encapsulate filtering criteria for operations on clients
46+ /* This struct is used to encapsulate filtering criteria for operations on clients
4947 * such as identifying specific clients to kill or retrieve. Each field in the struct
50- * represents a filter that can be applied based on specific attributes of a client.
51- */
48+ * represents a filter that can be applied based on specific attributes of a client. */
5249typedef struct {
53- /** A set of client IDs to filter. If NULL, no ID filtering is applied. */
50+ /* A set of client IDs to filter. If NULL, no ID filtering is applied. */
5451 intset * ids ;
55- /** Maximum age (in seconds) of a client connection for filtering.
52+ /* Maximum age (in seconds) of a client connection for filtering.
5653 * Connections younger than this value will not match.
5754 * A value of 0 means no age filtering. */
5855 long long max_age ;
59- /** Address/port of the client. If NULL, no address filtering is applied. */
56+ /* Address/port of the client. If NULL, no address filtering is applied. */
6057 char * addr ;
61- /** Remote address/port of the client. If NULL, no address filtering is applied. */
58+ /* Remote address/port of the client. If NULL, no address filtering is applied. */
6259 char * laddr ;
63- /** Filtering clients by authentication user. If NULL, no user-based filtering is applied. */
60+ /* Filtering clients by authentication user. If NULL, no user-based filtering is applied. */
6461 user * user ;
65- /** Client type to filter. If set to -1, no type filtering is applied. */
62+ /* Client type to filter. If set to -1, no type filtering is applied. */
6663 int type ;
67- /**< Boolean flag to determine if the current client (`me`) should be filtered. 1 means "skip me", 0 means otherwise. */
64+ /* Boolean flag to determine if the current client (`me`) should be filtered. 1 means "skip me", 0 means otherwise. */
6865 int skipme ;
6966} clientFilter ;
7067
@@ -73,7 +70,7 @@ static void pauseClientsByClient(mstime_t end, int isPauseClientAll);
7370int postponeClientRead (client * c );
7471char * getClientSockname (client * c );
7572int parseClientFilters (client * c , int i , clientFilter * filter );
76- bool clientMatchesFilter (client * client , clientFilter client_filter );
73+ int clientMatchesFilter (client * client , clientFilter client_filter );
7774sds getAllFilteredClientsInfoString (clientFilter * client_filter , int hide_user_data );
7875
7976int ProcessingEventsWhileBlocked = 0 ; /* See processEventsWhileBlocked(). */
@@ -3455,7 +3452,7 @@ sds getAllClientsInfoString(int type, int hide_user_data) {
34553452 listNode * ln ;
34563453 listIter li ;
34573454 client * client ;
3458- sds o = sdsnewlen (SDS_NOINIT , 500 );
3455+ sds o = sdsnewlen (SDS_NOINIT , 200 * listLength ( server . clients ) );
34593456 sdsclear (o );
34603457 listRewind (server .clients , & li );
34613458 while ((ln = listNext (& li )) != NULL ) {
@@ -3471,7 +3468,7 @@ sds getAllFilteredClientsInfoString(clientFilter *client_filter, int hide_user_d
34713468 listNode * ln ;
34723469 listIter li ;
34733470 client * client ;
3474- sds o = sdsnewlen (SDS_NOINIT , 200 * listLength ( server . clients ) );
3471+ sds o = sdsnewlen (SDS_NOINIT , 500 );
34753472 sdsclear (o );
34763473 listRewind (server .clients , & li );
34773474 while ((ln = listNext (& li )) != NULL ) {
@@ -3678,18 +3675,18 @@ int parseClientFilters(client *c, int i, clientFilter *filter) {
36783675 return C_OK ;
36793676}
36803677
3681- bool clientMatchesFilter (client * client , clientFilter client_filter ) {
3678+ int clientMatchesFilter (client * client , clientFilter client_filter ) {
36823679 // Check each filter condition and return false if the client does not match.
3683- if (client_filter .addr && strcmp (getClientPeerId (client ), client_filter .addr ) != 0 ) return false ;
3684- if (client_filter .laddr && strcmp (getClientSockname (client ), client_filter .laddr ) != 0 ) return false ;
3685- if (client_filter .type != -1 && getClientType (client ) != client_filter .type ) return false ;
3686- if (client_filter .ids && !intsetFind (client_filter .ids , client -> id )) return false ;
3687- if (client_filter .user && client -> user != client_filter .user ) return false ;
3688- if (client_filter .skipme && client == server .current_client ) return false ; // Skipme check
3689- if (client_filter .max_age != 0 && (long long )(commandTimeSnapshot () / 1000 - client -> ctime ) < client_filter .max_age ) return false ;
3680+ if (client_filter .addr && strcmp (getClientPeerId (client ), client_filter .addr ) != 0 ) return 0 ;
3681+ if (client_filter .laddr && strcmp (getClientSockname (client ), client_filter .laddr ) != 0 ) return 0 ;
3682+ if (client_filter .type != -1 && getClientType (client ) != client_filter .type ) return 0 ;
3683+ if (client_filter .ids && !intsetFind (client_filter .ids , client -> id )) return 0 ;
3684+ if (client_filter .user && client -> user != client_filter .user ) return 0 ;
3685+ if (client_filter .skipme && client == server .current_client ) return 0 ; // Skipme check
3686+ if (client_filter .max_age != 0 && (long long )(commandTimeSnapshot () / 1000 - client -> ctime ) < client_filter .max_age ) return 0 ;
36903687
36913688 // If all conditions are satisfied, the client matches the filter.
3692- return true ;
3689+ return 1 ;
36933690}
36943691
36953692void clientCommand (client * c ) {
@@ -3851,12 +3848,12 @@ void clientCommand(client *c) {
38513848
38523849 /* New style syntax: parse options. */
38533850 if (parseClientFilters (c , i , & client_filter ) != C_OK ) {
3854- zfree ( client_filter . ids ) ; // Free the intset on error
3851+ goto client_kill_done ; // Free the intset on error
38553852 return ;
38563853 }
38573854 } else {
3858- zfree (client_filter .ids ); // Free the intset on error
38593855 addReplyErrorObject (c , shared .syntaxerr );
3856+ goto client_kill_done ; // Free the intset on error
38603857 return ;
38613858 }
38623859
@@ -3888,6 +3885,7 @@ void clientCommand(client *c) {
38883885 /* If this client has to be closed, flag it as CLOSE_AFTER_REPLY
38893886 * only after we queued the reply to its output buffers. */
38903887 if (close_this_client ) c -> flag .close_after_reply = 1 ;
3888+ client_kill_done :
38913889 zfree (client_filter .ids );
38923890 } else if (!strcasecmp (c -> argv [1 ]-> ptr , "unblock" ) && (c -> argc == 3 || c -> argc == 4 )) {
38933891 /* CLIENT UNBLOCK <id> [timeout|error] */
0 commit comments