libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit e238f4f0134b62d1865e44d6c37c0705e7988254
parent e4bfe201e174bdc9660005756959a8b2eac3e0ac
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 15 Feb 2017 13:19:28 +0100

Fix(config_free): Now taking a pointer of pointer in args:

In order to free properly the pointer, without leak referencing by the caller.

Diffstat:
Minc/config.h | 4++--
Msrc/config.c | 13+++++++------
Mtests/test_config.c | 4++--
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/inc/config.h b/inc/config.h @@ -88,8 +88,8 @@ typedef struct s_config { } config_t; config_t *parse_config(const char *path, u8_t *ret); -void config_free(config_t *ptr); +void config_free(config_t **ptr); void config_error_cb(cfg_t *ptr, const char *fmt, va_list ap); -void config_get_error_string(config_t *ptr); +void config_get_error_string(config_t *ptr); #endif /* CONFIG_H */ diff --git a/src/config.c b/src/config.c @@ -115,12 +115,13 @@ config_t *parse_config(const char *path, u8_t *ret) { * \brief Free a config_t pointer * \param ptr Pointer to free */ -void config_free(config_t *ptr) { - if (ptr != NULL) { - cfg_free(ptr->ptr); - free(ptr->fn); - free(ptr->err); - free(ptr); +void config_free(config_t **ptr) { + if (ptr != NULL && *ptr != NULL) { + cfg_free((*ptr)->ptr); + free((*ptr)->fn); + free((*ptr)->err); + free(*ptr); + *ptr = NULL; } } diff --git a/tests/test_config.c b/tests/test_config.c @@ -9,7 +9,7 @@ TEST(config_basic) { m_panic("%s\n", conf->err); } TEST_ASSERT((ret == 0), "Can't open configuration file"); - config_free(conf); + config_free(&conf); return TEST_SUCCESS; } @@ -20,7 +20,7 @@ TEST(config_error) { conf = parse_config("conf/bad.conf", &ret); TEST_ASSERT((ret != 0), "No parsing error ?"); TEST_ASSERT((strcmp(conf->err, "conf/bad.conf:1: no such option 'nonsense'") == 0), "Error is bad."); - config_free(conf); + config_free(&conf); return TEST_SUCCESS; }