add support to free a stack based allocated kubeconfig_t

This commit is contained in:
DanyT
2024-07-03 18:48:51 +03:00
parent 34d1b13efe
commit 4a19bc2a5f
3 changed files with 28 additions and 19 deletions

View File

@@ -394,18 +394,14 @@ int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApi
static char fname[] = "load_kube_config()";
int rc = 0;
kubeconfig_t *kubeconfig = kubeconfig_create();
if (!kubeconfig) {
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
return -1;
}
kubeconfig_t kubeconfig;
memset(&kubeconfig, 0, sizeof(kubeconfig_t));
kubeconfig->fileName = getWorkingConfigFile(configFileName);
kubeconfig.fileName = getWorkingConfigFile(configFileName);
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig);
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, &kubeconfig);
kubeconfig_free(kubeconfig);
kubeconfig = NULL;
kubeconfig_free_members(&kubeconfig);
return rc;
}
@@ -415,18 +411,14 @@ int load_kube_config_buffer(char **pBasePath, sslConfig_t ** pSslConfig, list_t
static char fname[] = "load_kube_config_buffer()";
int rc = 0;
kubeconfig_t *kubeconfig = kubeconfig_create();
if (!kubeconfig) {
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
return -1;
}
kubeconfig_t kubeconfig;
memset(&kubeconfig, 0, sizeof(kubeconfig_t));
kubeconfig->buffer = strdup(buffer);
kubeconfig.buffer = strdup(buffer);
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig);
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, &kubeconfig);
kubeconfig_free(kubeconfig);
kubeconfig = NULL;
kubeconfig_free_members(&kubeconfig);
return rc;
}

View File

@@ -214,7 +214,7 @@ kubeconfig_t *kubeconfig_create()
return config;
}
void kubeconfig_free(kubeconfig_t * kubeconfig)
void kubeconfig_free_members(kubeconfig_t * kubeconfig)
{
if (!kubeconfig) {
return;
@@ -256,6 +256,15 @@ void kubeconfig_free(kubeconfig_t * kubeconfig)
kubeconfig_properties_free(kubeconfig->contexts, kubeconfig->contexts_count);
kubeconfig->contexts = NULL;
}
}
void kubeconfig_free(kubeconfig_t * kubeconfig)
{
if (!kubeconfig) {
return;
}
kubeconfig_free_members(kubeconfig);
free(kubeconfig);
}

View File

@@ -113,9 +113,17 @@ extern "C" {
kubeconfig_property_t **kubeconfig_properties_create(int contexts_count, kubeconfig_property_type_t type);
void kubeconfig_properties_free(kubeconfig_property_t ** properties, int properties_count);
// allocate kubeconfig_t structure on heap
kubeconfig_t *kubeconfig_create();
// free a kubeconfig_t structure allocated on heap by a call to kubeconfig_create
void kubeconfig_free(kubeconfig_t * kubeconfig);
// free internal members of a kubeconfig_t structure.
// used when releasing resources for a kubeconfig_t that was not allocated using kubeconfig_create
// for example a kubeconfig_t allocated on stack
void kubeconfig_free_members(kubeconfig_t * kubeconfig);
#ifdef __cplusplus
}
#endif