Skip to content

Commit

Permalink
Check for null pointer in dictRelease, listRelease and cluster_slot_d…
Browse files Browse the repository at this point in the history
…estroy (#113)

Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
  • Loading branch information
bjosv authored Oct 10, 2024
1 parent 432f689 commit 21b0827
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/adlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ void listRelease(hilist *list) {
unsigned long len;
listNode *current, *next;

if (list == NULL)
return;
current = list->head;
len = list->len;
while (len--) {
Expand Down
6 changes: 2 additions & 4 deletions src/async.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ static valkeyAsyncContext *valkeyAsyncInitialize(valkeyContext *c) {

return ac;
oom:
if (channels)
dictRelease(channels);
if (patterns)
dictRelease(patterns);
dictRelease(channels);
dictRelease(patterns);
return NULL;
}

Expand Down
51 changes: 15 additions & 36 deletions src/cluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,8 @@ static void freeValkeyClusterNode(valkeyClusterNode *node) {
node->acon->data = NULL;
valkeyAsyncFree(node->acon);
}
if (node->slots != NULL) {
listRelease(node->slots);
}
if (node->slaves != NULL) {
listRelease(node->slaves);
}
listRelease(node->slots);
listRelease(node->slaves);
vk_free(node);
}

Expand Down Expand Up @@ -337,6 +333,8 @@ static int cluster_slot_ref_node(cluster_slot *slot, valkeyClusterNode *node) {
}

static void cluster_slot_destroy(cluster_slot *slot) {
if (slot == NULL)
return;
slot->start = 0;
slot->end = 0;
slot->node = NULL;
Expand Down Expand Up @@ -849,12 +847,8 @@ static dict *parse_cluster_slots(valkeyClusterContext *cc, valkeyReply *reply) {
// passthrough

error:
if (nodes != NULL) {
dictRelease(nodes);
}
if (slot != NULL) {
cluster_slot_destroy(slot);
}
dictRelease(nodes);
cluster_slot_destroy(slot);
return NULL;
}

Expand Down Expand Up @@ -1037,9 +1031,7 @@ static dict *parse_cluster_nodes(valkeyClusterContext *cc, valkeyReply *reply) {
goto error;
}

if (nodes_name != NULL) {
dictRelease(nodes_name);
}
dictRelease(nodes_name);

return nodes;

Expand All @@ -1050,12 +1042,8 @@ static dict *parse_cluster_nodes(valkeyClusterContext *cc, valkeyReply *reply) {
error:
sdsfreesplitres(part, count_part);
sdsfreesplitres(slot_start_end, count_slot_start_end);
if (nodes != NULL) {
dictRelease(nodes);
}
if (nodes_name != NULL) {
dictRelease(nodes_name);
}
dictRelease(nodes);
dictRelease(nodes_name);
return NULL;
}

Expand Down Expand Up @@ -1233,9 +1221,8 @@ static int updateNodesAndSlotmap(valkeyClusterContext *cc, dict *nodes) {
* the release procedure might access cc->nodes. */
dict *oldnodes = cc->nodes;
cc->nodes = nodes;
if (oldnodes != NULL) {
dictRelease(oldnodes);
}
dictRelease(oldnodes);

if (cc->event_callback != NULL) {
cc->event_callback(cc, VALKEYCLUSTER_EVENT_SLOTMAP_UPDATED,
cc->event_privdata);
Expand Down Expand Up @@ -1322,14 +1309,8 @@ void valkeyClusterFree(valkeyClusterContext *cc) {
vk_free(cc->username);
vk_free(cc->password);
vk_free(cc->table);

if (cc->nodes != NULL) {
dictRelease(cc->nodes);
}

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

memset(cc, 0xff, sizeof(*cc));
vk_free(cc);
Expand Down Expand Up @@ -2705,10 +2686,8 @@ void valkeyClusterReset(valkeyClusterContext *cc) {
} while (reply != NULL);
}

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

if (cc->need_update_route) {
status = valkeyClusterUpdateSlotmap(cc);
Expand Down
2 changes: 2 additions & 0 deletions src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ static int _dictClear(dict *ht) {

/* Clear & Release the hash table */
void dictRelease(dict *ht) {
if (ht == NULL)
return;
_dictClear(ht);
vk_free(ht);
}
Expand Down

0 comments on commit 21b0827

Please sign in to comment.