B#13295: JSON mapping gets cause high CPU % and memory leak

This commit is contained in:
Amin Ben Romdhane 2024-01-15 11:30:57 +01:00
parent 3b63b5b614
commit 6dab2cf7b8
3 changed files with 28 additions and 24 deletions

View file

@ -13,7 +13,7 @@
#include "dmentry.h"
#define MAX_DM_LENGTH (1024)
#define json_object_get_string(x) (char *)json_object_get_string(x)
#define json_object_get_string(x) ((x != NULL) ? (char *)json_object_get_string(x) : "")
static LIST_HEAD(loaded_json_files);
static LIST_HEAD(json_list);
@ -384,7 +384,6 @@ static int browse_obj(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data,
}
if (type && strcmp(json_object_get_string(type), "uci") == 0) {
char buf_instance[128], buf_alias[128], object[64] = {0};
struct json_object *uci_obj = NULL;
struct json_object *file = NULL;
struct json_object *section = NULL;
@ -400,15 +399,6 @@ static int browse_obj(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data,
json_object_object_get_ex(section, "type", &section_type);
json_object_object_get_ex(uci_obj, "dmmapfile", &dmmap_file);
find_current_obj(parent_node->current_object, object, sizeof(object));
snprintf(buf_instance, sizeof(buf_instance), "%s_instance", object);
snprintf(buf_alias, sizeof(buf_alias), "%s_alias", object);
for (int i = 0; buf_instance[i]; i++)
buf_instance[i] = tolower(buf_instance[i]);
for (int i = 0; buf_alias[i]; i++)
buf_alias[i] = tolower(buf_alias[i]);
if (file && section_type && dmmap_file) {
synchronize_specific_config_sections_with_dmmap(json_object_get_string(file), json_object_get_string(section_type), json_object_get_string(dmmap_file), &dup_list);
list_for_each_entry(p, &dup_list, list) {
@ -420,7 +410,7 @@ static int browse_obj(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data,
continue;
}
inst = handle_instance(dmctx, parent_node, p->dmmap_section, buf_instance, buf_alias);
inst = handle_instance(dmctx, parent_node, p->dmmap_section, "instance", "alias");
if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)p->config_section, inst) == DM_STOP)
break;
@ -511,8 +501,6 @@ static int add_obj(char *refparam, struct dmctx *ctx, void *data, char **instanc
struct json_object *section = NULL;
struct json_object *section_type = NULL;
struct json_object *dmmap_file = NULL;
char object[64] = {0};
char buf_instance[128];
char sec_name[128];
json_object_object_get_ex((mapping_0 && json_version == JSON_VERSION_1) ? mapping_0 : mapping_obj, "uci", &uci_obj);
@ -521,27 +509,23 @@ static int add_obj(char *refparam, struct dmctx *ctx, void *data, char **instanc
json_object_object_get_ex(section, "type", &section_type);
json_object_object_get_ex(uci_obj, "dmmapfile", &dmmap_file);
find_current_obj(refparam, object, sizeof(object));
snprintf(buf_instance, sizeof(buf_instance), "%s_instance", object);
snprintf(sec_name, sizeof(sec_name), "%s%s%s_%s", data ? section_name((struct uci_section *)data) : "", data ? "_" : "", object, *instance);
char *uci_sec_name = json_object_get_string(section_type);
for (int i = 0; buf_instance[i]; i++)
buf_instance[i] = tolower(buf_instance[i]);
snprintf(sec_name, sizeof(sec_name), "%s%s%s_%s", data ? section_name((struct uci_section *)data) : "", data ? "_" : "", uci_sec_name, *instance);
for (int i = 0; sec_name[i]; i++)
sec_name[i] = tolower(sec_name[i]);
replace_special_char(sec_name, '_');
if (file && section_type && dmmap_file) {
struct uci_section *s = NULL, *dmmap_s = NULL;
dmuci_add_section(json_object_get_string(file), json_object_get_string(section_type), &s);
dmuci_add_section(json_object_get_string(file), uci_sec_name, &s);
dmuci_rename_section_by_section(s, sec_name);
if (data) dmuci_set_value_by_section(s, "dm_parent", section_name((struct uci_section *)data));
dmuci_add_section_bbfdm(json_object_get_string(dmmap_file), json_object_get_string(section_type), &dmmap_s);
dmuci_add_section_bbfdm(json_object_get_string(dmmap_file), uci_sec_name, &dmmap_s);
dmuci_set_value_by_section(dmmap_s, "section_name", section_name(s));
if (data) dmuci_set_value_by_section(dmmap_s, "dm_parent", section_name((struct uci_section *)data));
dmuci_set_value_by_section(dmmap_s, buf_instance, *instance);
dmuci_set_value_by_section(dmmap_s, "instance", *instance);
}
}

View file

@ -1870,6 +1870,25 @@ void remove_char(char *str, const char c)
str[j] = 0;
}
bool special_char(char c)
{
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
(c == '_'))
return false;
else
return true;
}
void replace_special_char(char *str, char c)
{
for (int i = 0; i < DM_STRLEN(str); i++) {
if (special_char(str[i]))
str[i] = c;
}
}
char *replace_char(char *str, char find, char replace)
{
char *current_pos = DM_STRCHR(str, find);

View file

@ -281,6 +281,7 @@ bool file_exists(const char *path);
bool is_regular_file(const char *path);
unsigned long file_system_size(const char *path, const enum fs_size_type_enum type);
void remove_char(char *str, const char c);
void replace_special_char(char *str, char c);
char *replace_char(char *str, char find, char replace);
char *replace_str(const char *str, const char *substr, const char *replacement);
int dm_file_to_buf(const char *filename, void *buf, size_t buf_size);