Fix use after free for linker_arg

elements in linker_arg are used in TCCState structure and must thus not
be freed when option parsing is finished. Declare linker_arg as a global
static variable and free it after tcc_delete has been called on TCCState
structure.

This fix commit 7fb0482a46
master
Thomas Preud'homme 2012-03-16 19:21:29 +01:00
parent c52d79605a
commit 2eee100c37
1 changed files with 2 additions and 2 deletions

4
tcc.c
View File

@ -35,6 +35,7 @@ static int do_bench = 0;
static int gen_deps;
static const char *deps_outfile;
static const char *m_option;
static char *linker_arg;
#define TCC_OPTION_HAS_ARG 0x0001
#define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
@ -280,7 +281,6 @@ static int parse_args(TCCState *s, int argc, char **argv)
const char *optarg, *p1, *r1;
char *r;
int was_pthread;
char *linker_arg = NULL;
unsigned long linker_argsize = 0;
was_pthread = 0; /* is set if commandline contains -pthread key */
@ -478,7 +478,6 @@ static int parse_args(TCCState *s, int argc, char **argv)
}
if ((r = (char *) tcc_set_linker(s, (char *)linker_arg, TRUE)))
tcc_error("unsupported linker option '%s'", r);
tcc_free(linker_arg);
/* fixme: these options could be different on your platform */
if (was_pthread && output_type != TCC_OUTPUT_OBJ) {
dynarray_add((void ***)&files, &nb_files, "-lpthread");
@ -611,6 +610,7 @@ int main(int argc, char **argv)
}
tcc_delete(s);
tcc_free(linker_arg);
tcc_free(outfile);
#ifdef MEM_DEBUG