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 // retuun -1: failed
static int parse_packet_io_section(toml_table_t *root, struct packet_io_options *opts) 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; const char *ptr;
toml_table_t *table; toml_table_t *table;
toml_array_t *mask_array; 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) if (table == NULL)
{ {
CONFIG_LOG_ERROR("config file missing packet_io section"); CONFIG_LOG_ERROR("config file missing packet_io section");
return -1; goto error_out;
} }
ptr = toml_raw_in(table, "mode"); ptr = toml_raw_in(table, "mode");
temp = NULL; if (ptr == NULL || toml_rtos(ptr, &ptr_mode) != 0)
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->mode"); 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; opts->mode = PACKET_IO_DUMPFILE;
} }
else if (strcmp(temp, "dumpfilelist") == 0) else if (strcmp(ptr_mode, "dumpfilelist") == 0)
{ {
opts->mode = PACKET_IO_DUMPFILELIST; opts->mode = PACKET_IO_DUMPFILELIST;
} }
else if (strcmp(temp, "marsio") == 0) else if (strcmp(ptr_mode, "marsio") == 0)
{ {
opts->mode = PACKET_IO_MARSIO; opts->mode = PACKET_IO_MARSIO;
} }
else else
{ {
CONFIG_LOG_ERROR("config file invalid packet_io->mode %s, only support dumpfile and marsio", ptr); 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) if (opts->mode == PACKET_IO_DUMPFILE || opts->mode == PACKET_IO_DUMPFILELIST)
{ {
ptr = toml_raw_in(table, "dumpfile_path"); ptr = toml_raw_in(table, "dumpfile_path");
temp = NULL; if (ptr == NULL || toml_rtos(ptr, &ptr_dumpfile_path) != 0)
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->dumpfile_path"); 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 else
{ {
ptr = toml_raw_in(table, "app_symbol"); ptr = toml_raw_in(table, "app_symbol");
temp = NULL; if (ptr == NULL || toml_rtos(ptr, &ptr_app_symbol) != 0)
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->app_symbol"); 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"); ptr = toml_raw_in(table, "dev_symbol");
temp = NULL; if (ptr == NULL || toml_rtos(ptr, &ptr_dev_symbol) != 0)
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->dev_symbol"); 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"); ptr = toml_raw_in(table, "nr_threads");
if (ptr == NULL) if (ptr == NULL)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->nr_threads"); CONFIG_LOG_ERROR("config file missing packet_io->nr_threads");
return -1; goto error_out;
} }
if (atoi(ptr) <= 0 || atoi(ptr) > MAX_THREAD_NUM) 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); 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); 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) if (mask_array == NULL)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask"); 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++) 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) if (ptr == NULL)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask[%d]", i); CONFIG_LOG_ERROR("config file missing packet_io->cpu_mask[%d]", i);
return -1; goto error_out;
} }
opts->cpu_mask[i] = atoi(ptr); 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 // return 0: success

View File

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

View File

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