Skip to content

Commit

Permalink
add support to free a stack based allocated kubeconfig_t
Browse files Browse the repository at this point in the history
  • Loading branch information
DanyT committed Jul 3, 2024
1 parent 34d1b13 commit 4a19bc2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
28 changes: 10 additions & 18 deletions kubernetes/config/kube_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion kubernetes/config/kube_config_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions kubernetes/config/kube_config_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4a19bc2

Please sign in to comment.