Fix memleak in db/formats/mangling/mangling

This commit is contained in:
Qijia Liu 2021-01-25 01:11:40 +00:00 committed by pancake
parent 07b466f61b
commit b8afdf8d7d
8 changed files with 71 additions and 57 deletions

View File

@ -188,6 +188,7 @@ R_API RAnal *r_anal_free(RAnal *a) {
free (a->os);
free (a->zign_path);
r_list_free (a->plugins);
r_list_free (a->esil_plugins);
r_rbtree_free (a->bb_tree, __block_free_rb, NULL);
r_spaces_fini (&a->meta_spaces);
r_spaces_fini (&a->zign_spaces);

View File

@ -221,6 +221,7 @@ static int get_template_params(const char *sym, size_t *amount_of_read_chars, ch
sym += 2;
size_t ret = get_namespace_and_name (sym, &str, NULL, true);
if (!ret) {
free_type_code_str_struct (&str);
return eDemanglerErrUncorrectMangledSymbol;
}
sym += ret + 1;
@ -1150,7 +1151,6 @@ static inline const char *get_calling_convention(char calling_convention) {
static EDemanglerErr parse_function_args(const char *sym, char **demangled_args, size_t *read_chars) {
EDemanglerErr err = eDemanglerErrOK;
bool is_abbr_type = false;
const char *curr_pos = sym;
size_t len = 0;
@ -1165,6 +1165,7 @@ static EDemanglerErr parse_function_args(const char *sym, char **demangled_args,
while (*curr_pos && *curr_pos != 'Z') {
if (*curr_pos != '@') {
char *tmp;
bool is_abbr_type = false;
if (len) {
copy_string (&func_str, ", ", 0);
}
@ -1172,6 +1173,7 @@ static EDemanglerErr parse_function_args(const char *sym, char **demangled_args,
if (err != eDemanglerErrOK) {
// abbreviation of type processing
if ((*curr_pos >= '0') && (*curr_pos <= '9')) {
free (tmp);
tmp = r_list_get_n (abbr_types, (ut32)(*curr_pos - '0'));
if (!tmp) {
err = eDemanglerErrUncorrectMangledSymbol;
@ -1182,6 +1184,7 @@ static EDemanglerErr parse_function_args(const char *sym, char **demangled_args,
is_abbr_type = true;
} else {
err = eDemanglerErrUncorrectMangledSymbol;
free (tmp);
break;
}
}
@ -1193,7 +1196,7 @@ static EDemanglerErr parse_function_args(const char *sym, char **demangled_args,
copy_string (&func_str, tmp, 0);
if (strncmp (tmp, "void", 4) == 0 && strlen (tmp) == 4) {
if (!strcmp (tmp, "void")) {
// arguments list is void
if (!is_abbr_type) {
free (tmp);
@ -1620,23 +1623,29 @@ static EDemanglerErr parse_data_type(const char *sym, SDataType *data_type, size
}
size_t i = get_namespace_and_name (curr_pos, &str, NULL, true);
if (!i) {
free_type_code_str_struct (&str);
return eDemanglerErrUncorrectMangledSymbol;
}
curr_pos += i;
if (*(curr_pos + 1) != '@') {
STypeCodeStr str2;
if (!init_type_code_str_struct (&str2)) {
free_type_code_str_struct (&str);
return eDemanglerErrMemoryAllocation;
}
i = get_namespace_and_name (curr_pos + 1, &str2, NULL, true);
if (!i) {
free_type_code_str_struct (&str);
free_type_code_str_struct (&str2);
return eDemanglerErrUncorrectMangledSymbol;
}
curr_pos += i + 1;
data_type->right = r_str_newf ("{for `%s's `%s'}", str.type_str, str2.type_str);
free_type_code_str_struct (&str2);
} else {
data_type->right = r_str_newf ("{for `%s'}", str.type_str);
}
free_type_code_str_struct (&str);
} else {
data_type->right = strdup ("");
}
@ -1664,6 +1673,7 @@ static EDemanglerErr parse_function_type(const char *sym, SDataType *data_type,
} \
data_type->left = modifier_str; \
data_type->right = r_str_newf ("`adjustor{%s}'", num); \
free (num);\
*is_implicit_this_pointer = true; \
curr_pos += state.amount_of_read_chars; \
break; \

View File

@ -64,7 +64,6 @@ static const char *help_msg_eco[] = {
NULL
};
static char *curtheme = "default";
static bool getNext = false;
static void cmd_eval_init(RCore *core, RCmdDesc *parent) {
@ -85,12 +84,12 @@ static bool load_theme(RCore *core, const char *path) {
return res;
}
static bool nextpal_item(RCore *core, int mode, const char *file, int ctr) {
static bool nextpal_item(RCore *core, PJ *pj, int mode, const char *file) {
const char *fn = r_str_lchr (file, '/');
if (!fn) fn = file;
switch (mode) {
case 'j': // json
r_cons_printf ("%s\"%s\"", ctr?",":"", fn);
pj_s (pj, fn);
break;
case 'l': // list
r_cons_println (fn);
@ -99,20 +98,20 @@ static bool nextpal_item(RCore *core, int mode, const char *file, int ctr) {
// TODO: move logic here
break;
case 'n': // next
if (curtheme && !strcmp (curtheme, "default")) {
curtheme = r_str_dup (curtheme, fn);
if (core->theme && !strcmp (core->theme, "default")) {
core->theme = r_str_dup (core->theme, fn);
getNext = false;
}
if (getNext) {
curtheme = r_str_dup (curtheme, fn);
core->theme = r_str_dup (core->theme, fn);
getNext = false;
return false;
} else if (curtheme) {
if (!strcmp (curtheme, fn)) {
} else if (core->theme) {
if (!strcmp (core->theme, fn)) {
getNext = true;
}
} else {
curtheme = r_str_dup (curtheme, fn);
core->theme = r_str_dup (core->theme, fn);
return false;
}
break;
@ -127,7 +126,7 @@ static bool cmd_load_theme(RCore *core, const char *_arg) {
return false;
}
if (!r_str_cmp (_arg, "default", strlen (_arg))) {
curtheme = strdup (_arg);
core->theme = r_str_dup (core->theme, _arg);
r_cons_pal_init (core->cons->context);
return true;
}
@ -143,10 +142,10 @@ static bool cmd_load_theme(RCore *core, const char *_arg) {
if (!load_theme (core, home)) {
if (load_theme (core, path)) {
curtheme = r_str_dup (curtheme, arg);
core->theme = r_str_dup (core->theme, arg);
} else {
if (load_theme (core, arg)) {
curtheme = r_str_dup (curtheme, arg);
core->theme = r_str_dup (core->theme, arg);
} else {
char *absfile = r_file_abspath (arg);
eprintf ("eco: cannot open colorscheme profile (%s)\n", absfile);
@ -173,8 +172,8 @@ static void list_themes_in_path(RList *list, const char *path) {
r_list_free (files);
}
R_API char *r_core_get_theme (void) {
return curtheme;
R_API char *r_core_get_theme (RCore *core) {
return core->theme;
}
R_API RList *r_core_list_themes(RCore *core) {
@ -203,13 +202,17 @@ static void nextpal(RCore *core, int mode) {
RListIter *iter;
const char *fn;
char *path = NULL;
int ctr = 0;
PJ *pj = NULL;
if (mode == 'j') {
pj = r_core_pj_new (core);
if (!pj) {
return;
}
pj_a (pj);
}
char *home = r_str_home (R2_HOME_THEMES R_SYS_DIR);
getNext = false;
if (mode == 'j') {
r_cons_printf ("[");
}
// spaguetti!
if (home) {
files = r_sys_dir (home);
@ -219,25 +222,25 @@ static void nextpal(RCore *core, int mode) {
if (*fn && *fn != '.') {
if (mode == 'p') {
const char *nfn = iter->n? iter->n->data: NULL;
if (!curtheme) {
if (!core->theme) {
free (home);
r_list_free (files);
return;
}
eprintf ("%s %s %s\n",
r_str_get (nfn),
r_str_get (curtheme),
r_str_get (core->theme),
r_str_get (fn));
if (nfn && !strcmp (nfn, curtheme)) {
if (nfn && !strcmp (nfn, core->theme)) {
r_list_free (files);
files = NULL;
free (curtheme);
curtheme = strdup (fn);
free (core->theme);
core->theme = strdup (fn);
R_FREE (home);
goto done;
}
} else {
if (!nextpal_item (core, mode, fn, ctr++)) {
if (!nextpal_item (core, pj, mode, fn)) {
r_list_free (files);
files = NULL;
R_FREE (home);
@ -261,22 +264,22 @@ static void nextpal(RCore *core, int mode) {
if (*fn && *fn != '.') {
if (mode == 'p') {
const char *nfn = iter->n? iter->n->data: NULL;
if (!curtheme) {
if (!core->theme) {
free (home);
r_list_free (files);
return;
}
eprintf ("%s %s %s\n",
r_str_get (nfn),
r_str_get (curtheme),
r_str_get (core->theme),
r_str_get (fn));
if (nfn && !strcmp (nfn, curtheme)) {
free (curtheme);
curtheme = strdup (fn);
if (nfn && !strcmp (nfn, core->theme)) {
free (core->theme);
core->theme = strdup (fn);
goto done;
}
} else { // next
if (!nextpal_item (core, mode, fn, ctr++)) {
if (!nextpal_item (core, pj, mode, fn)) {
goto done;
}
}
@ -288,21 +291,23 @@ static void nextpal(RCore *core, int mode) {
done:
free (path);
if (getNext) {
R_FREE (curtheme);
R_FREE (core->theme);
nextpal (core, mode);
return;
}
if (mode == 'l' && !curtheme && !r_list_empty (files)) {
if (mode == 'l' && !core->theme && !r_list_empty (files)) {
//nextpal (core, mode);
} else if (mode == 'n' || mode == 'p') {
if (curtheme) {
r_core_cmdf (core, "eco %s", curtheme);
if (core->theme) {
r_core_cmdf (core, "eco %s", core->theme);
}
}
r_list_free (files);
files = NULL;
if (mode == 'j') {
r_cons_printf ("]\n");
pj_end (pj);
r_cons_println (pj_string (pj));
pj_free (pj);
}
}
@ -381,10 +386,10 @@ static int cmd_eval(void *data, const char *input) {
free (k);
}
return true;
case 'x': // "ecox"
case 'x': // "ex"
// XXX we need headers for the cmd_xxx files.
return cmd_quit (data, "");
case 'j': // "ecoj"
case 'j': // "ej"
r_config_list (core->config, NULL, 'j');
break;
case 'v': // verbose
@ -407,29 +412,25 @@ static int cmd_eval(void *data, const char *input) {
} else if (input[2] == ' ') {
cmd_load_theme (core, input + 3);
} else if (input[2] == 'o') {
cmd_load_theme (core, r_core_get_theme ());
cmd_load_theme (core, core->theme);
} else if (input[2] == 'c' || input[2] == '.') {
r_cons_printf ("%s\n", r_core_get_theme ());
r_cons_printf ("%s\n", core->theme);
} else if (input[2] == '?') {
r_core_cmd_help (core, help_msg_eco);
} else if (input[2] == 'q') {
RList *themes_list = r_core_list_themes (core);
RListIter *th_iter;
const char *th;
r_list_foreach (themes_list, th_iter, th) {
r_cons_printf ("%s\n", th);
}
} else {
RList *themes_list = r_core_list_themes (core);
RListIter *th_iter;
const char *th;
r_list_foreach (themes_list, th_iter, th) {
if (curtheme && !strcmp (curtheme, th)) {
if (input[2] == 'q') {
r_cons_printf ("%s\n", th);
} else if (core->theme && !strcmp (core->theme, th)) {
r_cons_printf ("- %s\n", th);
} else {
r_cons_printf (" %s\n", th);
}
}
r_list_free (themes_list);
}
break;
case 's': r_cons_pal_show (); break; // "ecs"

View File

@ -2820,6 +2820,7 @@ R_API bool r_core_init(RCore *core) {
core->fixedarch = false;
core->fixedbits = false;
core->theme = strdup ("default");
/* initialize libraries */
core->cons = r_cons_new ();
if (core->cons->refcnt == 1) {
@ -3042,6 +3043,7 @@ R_API void r_core_fini(RCore *c) {
should probably need to add a r_config_free_payload callback */
r_cons_free ();
r_cons_singleton ()->teefile = NULL; // HACK
free (c->theme);
r_search_free (c->search);
r_flag_free (c->flags);
r_fs_free (c->fs);

View File

@ -4316,7 +4316,7 @@ R_API void r_core_visual_colors(RCore *core) {
}
r_cons_rgb_str (cstr, sizeof (cstr), &rcolor);
char *esc = strchr (cstr + 1, '\x1b');
char *curtheme = r_core_get_theme ();
char *curtheme = r_core_get_theme (core);
r_cons_printf ("# Use '.' to randomize current color and ':' to randomize palette\n");
r_cons_printf ("# Press '"Color_RED"rR"Color_GREEN"gG"Color_BLUE"bB"Color_RESET

View File

@ -335,7 +335,7 @@ struct r_core_t {
bool log_events; // core.c:cb_event_handler : log actions from events if cfg.log.events is set
RList *ropchain;
bool use_tree_sitter_r2cmd;
char *theme;
bool marks_init;
ut64 marks[UT8_MAX + 1];
@ -382,7 +382,7 @@ typedef int (*RCoreSearchCallback)(RCore *core, ut64 from, ut8 *buf, int len);
#ifdef R_API
//#define r_core_ncast(x) (RCore*)(size_t)(x)
R_API RList *r_core_list_themes(RCore *core);
R_API char *r_core_get_theme(void);
R_API char *r_core_get_theme(RCore *core);
R_API const char *r_core_get_section_name(RCore *core, ut64 addr);
R_API RCons *r_core_get_cons(RCore *core);
R_API RBin *r_core_get_bin(RCore *core);

View File

@ -561,7 +561,6 @@ R_API int r_main_rabin2(int argc, const char **argv) {
int rawstr = 0;
int fd = -1;
RCore core = {0};
RLib *l = NULL;
ut64 at = UT64_MAX;
r_core_init (&core);
@ -572,7 +571,7 @@ R_API int r_main_rabin2(int argc, const char **argv) {
char *plugindir = r_str_r2_prefix (R2_PLUGINS);
char *extrasdir = r_str_r2_prefix (R2_EXTRAS);
char *bindingsdir = r_str_r2_prefix (R2_BINDINGS);
l = r_lib_new (NULL, NULL);
RLib *l = r_lib_new (NULL, NULL);
r_lib_add_handler (l, R_LIB_TYPE_BIN, "bin plugins",
&__lib_bin_cb, &__lib_bin_dt, bin);
r_lib_add_handler (l, R_LIB_TYPE_BIN_XTR, "bin xtr plugins",
@ -593,6 +592,7 @@ R_API int r_main_rabin2(int argc, const char **argv) {
free (extrasdir);
free (bindingsdir);
free (path);
r_lib_free (l);
}
free (tmp);
@ -1194,7 +1194,6 @@ R_API int r_main_rabin2(int argc, const char **argv) {
pj_free (pj);
r_cons_flush ();
r_core_fini (&core);
r_lib_free (l);
return 0;
}

View File

@ -847,8 +847,9 @@ R_API char *r_str_ndup(const char *ptr, int len) {
// TODO: deprecate?
R_API char *r_str_dup(char *ptr, const char *string) {
free (ptr);
return r_str_new (string);
char *str = r_str_new (string);
free (ptr); // in case ptr == string
return str;
}
R_API char *r_str_prepend(char *ptr, const char *string) {