Set fields to NULL after free in freeClient() (#1279)

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 <quchen@amazon.com>
This commit is contained in:
Qu Chen 2024-11-11 01:39:48 -08:00 committed by GitHub
parent 0b5b2c7484
commit 9300a7ebc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1731,6 +1731,7 @@ 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);
@ -1738,16 +1739,22 @@ void freeClient(client *c) {
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