Skip to content

Commit 4a19bc2

Browse files
committed
add support to free a stack based allocated kubeconfig_t
1 parent 34d1b13 commit 4a19bc2

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

kubernetes/config/kube_config.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,14 @@ int load_kube_config(char **pBasePath, sslConfig_t ** pSslConfig, list_t ** pApi
394394
static char fname[] = "load_kube_config()";
395395
int rc = 0;
396396

397-
kubeconfig_t *kubeconfig = kubeconfig_create();
398-
if (!kubeconfig) {
399-
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
400-
return -1;
401-
}
397+
kubeconfig_t kubeconfig;
398+
memset(&kubeconfig, 0, sizeof(kubeconfig_t));
402399

403-
kubeconfig->fileName = getWorkingConfigFile(configFileName);
400+
kubeconfig.fileName = getWorkingConfigFile(configFileName);
404401

405-
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig);
402+
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, &kubeconfig);
406403

407-
kubeconfig_free(kubeconfig);
408-
kubeconfig = NULL;
404+
kubeconfig_free_members(&kubeconfig);
409405

410406
return rc;
411407
}
@@ -415,18 +411,14 @@ int load_kube_config_buffer(char **pBasePath, sslConfig_t ** pSslConfig, list_t
415411
static char fname[] = "load_kube_config_buffer()";
416412
int rc = 0;
417413

418-
kubeconfig_t *kubeconfig = kubeconfig_create();
419-
if (!kubeconfig) {
420-
fprintf(stderr, "%s: Cannot create kubeconfig.[%s]\n", fname, strerror(errno));
421-
return -1;
422-
}
414+
kubeconfig_t kubeconfig;
415+
memset(&kubeconfig, 0, sizeof(kubeconfig_t));
423416

424-
kubeconfig->buffer = strdup(buffer);
417+
kubeconfig.buffer = strdup(buffer);
425418

426-
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, kubeconfig);
419+
rc = load_kube_config_common(pBasePath, pSslConfig, pApiKeys, &kubeconfig);
427420

428-
kubeconfig_free(kubeconfig);
429-
kubeconfig = NULL;
421+
kubeconfig_free_members(&kubeconfig);
430422

431423
return rc;
432424
}

kubernetes/config/kube_config_model.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ kubeconfig_t *kubeconfig_create()
214214
return config;
215215
}
216216

217-
void kubeconfig_free(kubeconfig_t * kubeconfig)
217+
void kubeconfig_free_members(kubeconfig_t * kubeconfig)
218218
{
219219
if (!kubeconfig) {
220220
return;
@@ -256,6 +256,15 @@ void kubeconfig_free(kubeconfig_t * kubeconfig)
256256
kubeconfig_properties_free(kubeconfig->contexts, kubeconfig->contexts_count);
257257
kubeconfig->contexts = NULL;
258258
}
259+
}
260+
261+
void kubeconfig_free(kubeconfig_t * kubeconfig)
262+
{
263+
if (!kubeconfig) {
264+
return;
265+
}
266+
267+
kubeconfig_free_members(kubeconfig);
259268

260269
free(kubeconfig);
261270
}

kubernetes/config/kube_config_model.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,17 @@ extern "C" {
113113
kubeconfig_property_t **kubeconfig_properties_create(int contexts_count, kubeconfig_property_type_t type);
114114
void kubeconfig_properties_free(kubeconfig_property_t ** properties, int properties_count);
115115

116+
// allocate kubeconfig_t structure on heap
116117
kubeconfig_t *kubeconfig_create();
118+
119+
// free a kubeconfig_t structure allocated on heap by a call to kubeconfig_create
117120
void kubeconfig_free(kubeconfig_t * kubeconfig);
118121

122+
// free internal members of a kubeconfig_t structure.
123+
// used when releasing resources for a kubeconfig_t that was not allocated using kubeconfig_create
124+
// for example a kubeconfig_t allocated on stack
125+
void kubeconfig_free_members(kubeconfig_t * kubeconfig);
126+
119127
#ifdef __cplusplus
120128
}
121129
#endif

0 commit comments

Comments
 (0)