bugfix toml_rtos memleak

This commit is contained in:
luwenpeng
2024-08-23 17:19:05 +08:00
parent a6223eacfe
commit 8db2e70c38
3 changed files with 86 additions and 51 deletions

View File

@@ -4,6 +4,7 @@
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include <stdlib.h>
#include <pthread.h>
#include "log.h"
@@ -93,10 +94,12 @@ static int parse_config(struct log_config *config, const char *cfg_file)
int ret = -1;
FILE *fp = NULL;
char errbuf[200];
char *temp;
char *ptr_output = NULL;
char *ptr_file = NULL;
char *ptr_level = NULL;
const char *ptr;
toml_table_t *log_section = NULL;
toml_table_t *conf_table = NULL;
toml_table_t *section = NULL;
toml_table_t *table = NULL;
fp = fopen(cfg_file, "r");
if (fp == NULL)
@@ -105,37 +108,36 @@ static int parse_config(struct log_config *config, const char *cfg_file)
goto error_out;
}
conf_table = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (conf_table == NULL)
table = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (table == NULL)
{
fprintf(stderr, "parse config file %s failed, %s\n", cfg_file, errbuf);
goto error_out;
}
log_section = toml_table_in(conf_table, "log");
if (log_section == NULL)
section = toml_table_in(table, "log");
if (section == NULL)
{
fprintf(stderr, "config file %s missing log section\n", cfg_file);
goto error_out;
}
// output
ptr = toml_raw_in(log_section, "output");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
ptr = toml_raw_in(section, "output");
if (ptr == NULL || toml_rtos(ptr, &ptr_output) != 0)
{
fprintf(stderr, "config file %s missing log.output\n", cfg_file);
goto error_out;
}
if (strcasecmp(temp, "stderr") == 0)
if (strcasecmp(ptr_output, "stderr") == 0)
{
config->output = LOG_OUTPUT_STDERR;
}
else if (strcasecmp(temp, "file") == 0)
else if (strcasecmp(ptr_output, "file") == 0)
{
config->output = LOG_OUTPUT_FILE;
}
else if (strcasecmp(temp, "both") == 0)
else if (strcasecmp(ptr_output, "both") == 0)
{
config->output = LOG_OUTPUT_BOTH;
}
@@ -147,25 +149,23 @@ static int parse_config(struct log_config *config, const char *cfg_file)
if (config->output == LOG_OUTPUT_FILE || config->output == LOG_OUTPUT_BOTH)
{
ptr = toml_raw_in(log_section, "file");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
ptr = toml_raw_in(section, "file");
if (ptr == NULL || toml_rtos(ptr, &ptr_file) != 0)
{
fprintf(stderr, "config file %s missing log.file\n", cfg_file);
goto error_out;
}
strcpy(config->log_file, temp);
strcpy(config->log_file, ptr_file);
}
// level
ptr = toml_raw_in(log_section, "level");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
ptr = toml_raw_in(section, "level");
if (ptr == NULL || toml_rtos(ptr, &ptr_level) != 0)
{
fprintf(stderr, "config file %s missing log.level\n", cfg_file);
goto error_out;
}
config->level = check_level(temp);
config->level = check_level(ptr_level);
if (config->level == LOG_NONE)
{
fprintf(stderr, "config file %s invalid log.level\n", cfg_file);
@@ -175,11 +175,23 @@ static int parse_config(struct log_config *config, const char *cfg_file)
ret = 0;
error_out:
if (conf_table)
{
toml_free(conf_table);
}
if (ptr_output)
{
free(ptr_output);
}
if (ptr_file)
{
free(ptr_file);
}
if (ptr_level)
{
free(ptr_level);
}
if (table)
{
toml_free(table);
}
if (fp)
{
fclose(fp);