fix: free cluster_config on error paths in load_primary_config; fix get_default_conf wrapper leak

This commit is contained in:
Rongsong Shen 2026-05-13 16:11:58 +00:00
parent cd02cf40d2
commit 6141264219

View file

@ -139,16 +139,17 @@ int load_cluster_config(cluster_t* c,
cluster_config_t* get_default_conf(void* cl)
{
struct cluster_configuration* cf = NULL;
cluster_t* cls = (cluster_t*)cl;
struct cluster_configuration* cf = NULL;
load_cluster_config(cls, &cf);
if (cf == NULL) {
if (load_cluster_config(cls, &cf) != 0 || cf == NULL) {
PERROR("load_cluster_config");
return NULL;
}
return cf->cluster;
cluster_config_t *result = cf->cluster;
free(cf); /* free the wrapper; result (cf->cluster) is caller-owned */
return result;
}
/* ── load_primary_config ────────────────────────── */
@ -194,6 +195,13 @@ int load_primary_config(cluster_t *c)
memcpy(&tag, myconf->config_sha256, 8);
myconf->config_version_tag = be64toh(tag);
/* Free any prior decoded config before overwriting the pointer */
if (myconf->cluster_config) {
free_ClusterConfig(myconf->cluster_config);
free(myconf->cluster_config);
myconf->cluster_config = NULL;
}
/* Decode Cap'n Proto directly into myconf->cluster_config */
rc = load_cluster_config_from_file(myconf->conffile, &myconf->cluster_config);
if (rc != 0) {
@ -221,6 +229,11 @@ int load_primary_config(cluster_t *c)
if (config_contains_reserved_name(myconf->cluster_config)) {
FATAL("Config '%s' contains reserved name 'lost_and_found'",
myconf->conffile);
free_ClusterConfig(myconf->cluster_config);
free(myconf->cluster_config);
myconf->cluster_config = NULL;
free(myconf->conffile);
myconf->conffile = NULL;
return -EINVAL;
}