Skip to content

Commit

Permalink
Set fields to NULL after free in freeClient() (#1279)
Browse files Browse the repository at this point in the history
Null out several references after freeing the object in `freeClient()`.

This is just to make the code more safe, to protect against
use-after-free for future changes.

Signed-off-by: Qu Chen <[email protected]>
  • Loading branch information
QuChen88 authored Nov 11, 2024
1 parent 0b5b2c7 commit 9300a7e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -1731,23 +1731,30 @@ void freeClient(client *c) {
/* UNWATCH all the keys */
unwatchAllKeys(c);
listRelease(c->watched_keys);
c->watched_keys = NULL;

/* Unsubscribe from all the pubsub channels */
pubsubUnsubscribeAllChannels(c, 0);
pubsubUnsubscribeShardAllChannels(c, 0);
pubsubUnsubscribeAllPatterns(c, 0);
unmarkClientAsPubSub(c);
dictRelease(c->pubsub_channels);
c->pubsub_channels = NULL;
dictRelease(c->pubsub_patterns);
c->pubsub_patterns = NULL;
dictRelease(c->pubsubshard_channels);
c->pubsubshard_channels = NULL;

/* Free data structures. */
listRelease(c->reply);
c->reply = NULL;
zfree(c->buf);
c->buf = NULL;
freeReplicaReferencedReplBuffer(c);
freeClientArgv(c);
freeClientOriginalArgv(c);
if (c->deferred_reply_errors) listRelease(c->deferred_reply_errors);
c->deferred_reply_errors = NULL;
#ifdef LOG_REQ_RES
reqresReset(c, 1);
#endif
Expand Down

0 comments on commit 9300a7e

Please sign in to comment.