Skip to content

Commit

Permalink
Cleanup of cluster free functions
Browse files Browse the repository at this point in the history
Remove special handling when freeing the nodes dict. This is now
prevented by the newly added flag VALKEYCLUSTER_FLAG_DISCONNECTING.

Scrub the valkeyClusterContext before freeing to help us detect use-after-free.
This is similar to how valkeyContext is freed.

Signed-off-by: Björn Svensson <[email protected]>
  • Loading branch information
bjosv committed Sep 10, 2024
1 parent f888a14 commit 73292c2
Showing 1 changed file with 9 additions and 38 deletions.
47 changes: 9 additions & 38 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,6 @@ valkeyClusterContext *valkeyClusterContextInit(void) {
}

void valkeyClusterFree(valkeyClusterContext *cc) {

if (cc == NULL)
return;

Expand All @@ -1373,45 +1372,21 @@ void valkeyClusterFree(valkeyClusterContext *cc) {
cc->event_privdata);
}

if (cc->connect_timeout) {
vk_free(cc->connect_timeout);
cc->connect_timeout = NULL;
}

if (cc->command_timeout) {
vk_free(cc->command_timeout);
cc->command_timeout = NULL;
}

if (cc->table != NULL) {
vk_free(cc->table);
cc->table = NULL;
}
vk_free(cc->connect_timeout);
vk_free(cc->command_timeout);
vk_free(cc->username);
vk_free(cc->password);
vk_free(cc->table);

if (cc->nodes != NULL) {
/* Clear cc->nodes before releasing the dict since the release procedure
might access cc->nodes. When a node and its valkey context are freed
all pending callbacks are executed. Clearing cc->nodes prevents a pending
slotmap update command callback to trigger additional slotmap updates. */
dict *nodes = cc->nodes;
cc->nodes = NULL;
dictRelease(nodes);
dictRelease(cc->nodes);
}

if (cc->requests != NULL) {
listRelease(cc->requests);
}

if (cc->username != NULL) {
vk_free(cc->username);
cc->username = NULL;
}

if (cc->password != NULL) {
vk_free(cc->password);
cc->password = NULL;
}

memset(cc, 0xff, sizeof(*cc));
vk_free(cc);
}

Expand Down Expand Up @@ -3673,15 +3648,11 @@ void valkeyClusterAsyncDisconnect(valkeyClusterAsyncContext *acc) {
}

void valkeyClusterAsyncFree(valkeyClusterAsyncContext *acc) {
valkeyClusterContext *cc;

if (acc == NULL) {
if (acc == NULL)
return;
}

cc = acc->cc;
valkeyClusterContext *cc = acc->cc;
cc->flags |= VALKEYCLUSTER_FLAG_DISCONNECTING;

valkeyClusterFree(cc);

vk_free(acc);
Expand Down

0 comments on commit 73292c2

Please sign in to comment.