Skip to content

Commit

Permalink
Fixed memory leaks in resolve_stack_dump
Browse files Browse the repository at this point in the history
- Remove memory leaks reported by safemalloc
- Changed that all 0x strings are converted. This is needed
  to easily be able to resolve safemalloc backtraces
  • Loading branch information
montywi committed Apr 18, 2020
1 parent 48eda61 commit 749b988
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions extra/resolve_stack_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static struct my_option my_long_options[] =
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
{"version", 'V', "Output version information and exit.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
{"symbols-file", 's', "Use specified symbols file.", &sym_fname,
{"symbols-file", 's', "Use specified symbols file", &sym_fname,
&sym_fname, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"numeric-dump-file", 'n', "Read the dump from specified file.",
&dump_fname, &dump_fname, 0, GET_STR, REQUIRED_ARG,
Expand All @@ -63,7 +63,7 @@ static struct my_option my_long_options[] =


static void verify_sort();

static void clean_up();

static void print_version(void)
{
Expand Down Expand Up @@ -97,9 +97,18 @@ static void die(const char* fmt, ...)
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
clean_up();
my_end(0);
exit(1);
}

void local_exit(int error)
{
clean_up();
my_end(0);
exit(error);
}


static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
Expand All @@ -108,10 +117,12 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
switch(optid) {
case 'V':
print_version();
exit(0);
local_exit(0);
break;
case '?':
usage();
exit(0);
local_exit(0);
break;
}
return 0;
}
Expand All @@ -122,7 +133,7 @@ static int parse_args(int argc, char **argv)
int ho_error;

if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
exit(ho_error);
local_exit(ho_error);

/*
The following code is to make the command compatible with the old
Expand All @@ -143,13 +154,13 @@ static int parse_args(int argc, char **argv)
else
{
usage();
exit(1);
local_exit(1);
}
}
else if (argc != 0 || !sym_fname)
{
usage();
exit(1);
local_exit(1);
}
return 0;
}
Expand Down Expand Up @@ -242,6 +253,10 @@ static void init_sym_table()
static void clean_up()
{
delete_dynamic(&sym_table);
if (fp_dump && fp_dump != stdin)
my_fclose(fp_dump, MYF(0));
if (fp_sym)
my_fclose(fp_sym, MYF(0));
}

static void verify_sort()
Expand Down Expand Up @@ -283,7 +298,7 @@ static SYM_ENTRY* resolve_addr(uchar* addr, SYM_ENTRY* se)


/*
Resolve anything that starts with [0x or (+0x or start of line and 0x
Resolve anything that starts with [0x or (+0x or 0x
Skip '_end' as this is an indication of a wrong symbol (stack?)
*/

Expand All @@ -299,9 +314,7 @@ static void do_resolve()
found= 3;
if (p[0] == '(' && p[1] == '+' && p[2] == '0' && p[3] == 'x')
found= 4;

/* For stdin */
if (p == buf && p[0] == '0' && p[1] == 'x')
if (p[0] == '0' && p[1] == 'x')
found= 2;

if (found)
Expand All @@ -312,14 +325,15 @@ static void do_resolve()
addr= (uchar*)read_addr(&tmp);
if (resolve_addr(addr, &se) && strcmp(se.symbol, "_end"))
{
fprintf(fp_out, "%c%p %s + %d", *p, addr, se.symbol,
(int) (addr - se.addr));
found-= 2; /* Don't print 0x as it's added by %p */
while (found--)
fputc(*p++, stdout);
fprintf(fp_out, "%p %s + %d", addr,
se.symbol, (int) (addr - se.addr));
p= tmp-1;
}
else
{
fputc(*p, stdout);
}
}
else
fputc(*p, stdout);
Expand All @@ -336,5 +350,6 @@ int main(int argc, char** argv)
init_sym_table();
do_resolve();
clean_up();
my_end(0);
return 0;
}

0 comments on commit 749b988

Please sign in to comment.