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