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

@@ -46,7 +46,11 @@ static int parse_snowflake_section(toml_table_t *root, struct snowflake_options
// retuun -1: failed
static int parse_packet_io_section(toml_table_t *root, struct packet_io_options *opts)
{
char *temp;
int ret = -1;
char *ptr_mode = NULL;
char *ptr_dumpfile_path = NULL;
char *ptr_app_symbol = NULL;
char *ptr_dev_symbol = NULL;
const char *ptr;
toml_table_t *table;
toml_array_t *mask_array;
@@ -55,76 +59,72 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options
if (table == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io section");
return -1;
goto error_out;
}
ptr = toml_raw_in(table, "mode");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
if (ptr == NULL || toml_rtos(ptr, &ptr_mode) != 0)
{
CONFIG_LOG_ERROR("config file missing packet_io->mode");
return -1;
goto error_out;
}
if (strcmp(temp, "dumpfile") == 0)
if (strcmp(ptr_mode, "dumpfile") == 0)
{
opts->mode = PACKET_IO_DUMPFILE;
}
else if (strcmp(temp, "dumpfilelist") == 0)
else if (strcmp(ptr_mode, "dumpfilelist") == 0)
{
opts->mode = PACKET_IO_DUMPFILELIST;
}
else if (strcmp(temp, "marsio") == 0)
else if (strcmp(ptr_mode, "marsio") == 0)
{
opts->mode = PACKET_IO_MARSIO;
}
else
{
CONFIG_LOG_ERROR("config file invalid packet_io->mode %s, only support dumpfile and marsio", ptr);
return -1;
goto error_out;
}
if (opts->mode == PACKET_IO_DUMPFILE || opts->mode == PACKET_IO_DUMPFILELIST)
{
ptr = toml_raw_in(table, "dumpfile_path");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
if (ptr == NULL || toml_rtos(ptr, &ptr_dumpfile_path) != 0)
{
CONFIG_LOG_ERROR("config file missing packet_io->dumpfile_path");
return -1;
goto error_out;
}
strcpy(opts->dumpfile_path, temp);
strcpy(opts->dumpfile_path, ptr_dumpfile_path);
}
else
{
ptr = toml_raw_in(table, "app_symbol");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
if (ptr == NULL || toml_rtos(ptr, &ptr_app_symbol) != 0)
{
CONFIG_LOG_ERROR("config file missing packet_io->app_symbol");
return -1;
goto error_out;
}
strcpy(opts->app_symbol, temp);
strcpy(opts->app_symbol, ptr_app_symbol);
ptr = toml_raw_in(table, "dev_symbol");
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
if (ptr == NULL || toml_rtos(ptr, &ptr_dev_symbol) != 0)
{
CONFIG_LOG_ERROR("config file missing packet_io->dev_symbol");
return -1;
goto error_out;
}
strcpy(opts->dev_symbol, temp);
strcpy(opts->dev_symbol, ptr_dev_symbol);
}
ptr = toml_raw_in(table, "nr_threads");
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->nr_threads");
return -1;
goto error_out;
}
if (atoi(ptr) <= 0 || atoi(ptr) > MAX_THREAD_NUM)
{
CONFIG_LOG_ERROR("config file invalid packet_io->nr_threads %d, range [1, %d]", atoi(ptr), MAX_THREAD_NUM);
return -1;
goto error_out;
}
opts->nr_threads = atoi(ptr);
@@ -132,7 +132,7 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options
if (mask_array == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask");
return -1;
goto error_out;
}
for (uint16_t i = 0; i < opts->nr_threads; i++)
{
@@ -140,12 +140,32 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options
if (ptr == NULL)
{
CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask[%d]", i);
return -1;
goto error_out;
}
opts->cpu_mask[i] = atoi(ptr);
}
return 0;
ret = 0;
error_out:
if (ptr_mode)
{
free(ptr_mode);
}
if (ptr_dumpfile_path)
{
free(ptr_dumpfile_path);
}
if (ptr_app_symbol)
{
free(ptr_app_symbol);
}
if (ptr_dev_symbol)
{
free(ptr_dev_symbol);
}
return ret;
}
// return 0: success

View File

@@ -627,6 +627,9 @@ void stellar_free(struct stellar *st)
stellar_stat_free(runtime->stat);
STELLAR_LOG_FATAL("stellar exit\n");
log_free();
free(st);
st = NULL;
}
}

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);