From d04fc01de2298e2b7e405f499a919d1bd4d551f1 Mon Sep 17 00:00:00 2001 From: Jacek Piasecki Date: Thu, 26 Oct 2017 08:21:09 +0200 Subject: [PATCH] cfgfile: fix leak on creation error Unsuccesfull memory allocation for elements inside cfgfile structure could result in resource leak. Fixed by pointer verification after each malloc, if malloc fail - error branch is proceeded with freeing memory. Coverity issue: 195032 Fixes: d4cb8197589d ("cfgfile: support runtime modification") Signed-off-by: Jacek Piasecki Acked-by: Michal Jastrzebski --- lib/librte_cfgfile/rte_cfgfile.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/librte_cfgfile/rte_cfgfile.c b/lib/librte_cfgfile/rte_cfgfile.c index 80077b6448..eacf93a8c3 100644 --- a/lib/librte_cfgfile/rte_cfgfile.c +++ b/lib/librte_cfgfile/rte_cfgfile.c @@ -303,7 +303,7 @@ rte_cfgfile_create(int flags) CFG_ALLOC_SECTION_BATCH); if (cfg->sections == NULL) - return NULL; + goto error1; cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH; @@ -312,7 +312,7 @@ rte_cfgfile_create(int flags) struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH); if (cfg->sections[i].entries == NULL) - return NULL; + goto error1; cfg->sections[i].num_entries = 0; cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH; @@ -320,7 +320,21 @@ rte_cfgfile_create(int flags) if (flags & CFG_FLAG_GLOBAL_SECTION) rte_cfgfile_add_section(cfg, "GLOBAL"); + return cfg; +error1: + if (cfg->sections != NULL) { + for (i = 0; i < cfg->allocated_sections; i++) { + if (cfg->sections[i].entries != NULL) { + free(cfg->sections[i].entries); + cfg->sections[i].entries = NULL; + } + } + free(cfg->sections); + cfg->sections = NULL; + } + free(cfg); + return NULL; } int -- 2.20.1