mems & leaks

- define_start: set above preprocess_start because now
  preprocess_start is defining macros.

- free "cmd_include_files"
- free defines always (after error-longjmps)
- close all files (after error-longjmps)
- tccpe.c: free imports always
- libtcc.c: call tcc_memstats only after all states have
  been deleted.
This commit is contained in:
grischka 2017-02-13 18:23:55 +01:00
parent a4a20360e9
commit 13056da039
4 changed files with 38 additions and 14 deletions

View File

@ -32,6 +32,8 @@ ST_DATA int tcc_ext = 1;
/* XXX: get rid of this ASAP */
ST_DATA struct TCCState *tcc_state;
static int nb_states;
/********************************************************/
#ifdef ONE_SOURCE
@ -509,6 +511,7 @@ static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
/* default case: stderr */
if (s1->ppfp) /* print a newline during tcc -E */
fprintf(s1->ppfp, "\n"), fflush(s1->ppfp);
fflush(stdout); /* flush -v output */
fprintf(stderr, "%s\n", buf);
fflush(stderr); /* print error/warning now (win32) */
} else {
@ -623,17 +626,18 @@ static int tcc_compile(TCCState *s1)
{
Sym *define_start;
preprocess_start(s1);
define_start = define_stack;
if (setjmp(s1->error_jmp_buf) == 0) {
s1->nb_errors = 0;
s1->error_set_jmp_enabled = 1;
preprocess_start(s1);
tccgen_start(s1);
#ifdef INC_DEBUG
printf("%s: **** new file\n", file->filename);
#endif
ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
@ -641,13 +645,17 @@ static int tcc_compile(TCCState *s1)
decl(VT_CONST);
if (tok != TOK_EOF)
expect("declaration");
/* reset define stack, but keep -D and built-ins */
/* free defines here already on behalf of of M.M.'s possibly existing
experimental preprocessor implementation. The normal call below
is still there to free after error-longjmp */
free_defines(define_start);
tccgen_end(s1);
}
s1->error_set_jmp_enabled = 0;
free_inline_functions(s1);
/* reset define stack, but keep -D and built-ins */
free_defines(define_start);
sym_pop(&global_stack, NULL, 0);
sym_pop(&local_stack, NULL, 0);
return s1->nb_errors != 0 ? -1 : 0;
@ -724,6 +732,7 @@ LIBTCCAPI TCCState *tcc_new(void)
if (!s)
return NULL;
tcc_state = s;
++nb_states;
s->alacarte_link = 1;
s->nocommon = 1;
@ -897,6 +906,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
dynarray_reset(&s1->cmd_include_files, &s1->nb_cmd_include_files);
tcc_free(s1->tcc_lib_path);
tcc_free(s1->soname);
@ -915,7 +925,8 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
#endif
tcc_free(s1);
tcc_memstats(bench);
if (0 == --nb_states)
tcc_memstats(bench);
}
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)

View File

@ -999,14 +999,13 @@ ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess)
Sym *define_start;
int ret;
define_start = define_stack;
preprocess_start(s1);
/* default section is text */
cur_text_section = text_section;
ind = cur_text_section->data_offset;
define_start = define_stack;
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
symbols can be safely used */
put_elf_sym(symtab_section, 0, 0,

14
tccpe.c
View File

@ -770,6 +770,16 @@ found_dll:
return s;
}
void pe_free_imports(struct pe_info *pe)
{
int i;
for (i = 0; i < pe->imp_count; ++i) {
struct pe_import_info *p = pe->imp_info[i];
dynarray_reset(&p->symbols, &p->sym_count);
}
dynarray_reset(&pe->imp_info, &pe->imp_count);
}
/*----------------------------------------------------------------------------*/
static void pe_build_imports(struct pe_info *pe)
{
@ -861,9 +871,7 @@ static void pe_build_imports(struct pe_info *pe)
ent_ptr += sizeof (ADDR3264);
}
dll_ptr += sizeof(IMAGE_IMPORT_DESCRIPTOR);
dynarray_reset(&p->symbols, &p->sym_count);
}
dynarray_reset(&pe->imp_info, &pe->imp_count);
}
/* ------------------------------------------------------------- */
@ -1897,6 +1905,8 @@ ST_FUNC int pe_output_file(TCCState *s1, const char *filename)
#endif
}
pe_free_imports(&pe);
#ifdef PE_PRINT_SECTIONS
pe_print_sections(s1, "tcc.log");
#endif

16
tccpp.c
View File

@ -1342,7 +1342,8 @@ ST_FUNC void free_defines(Sym *b)
sym_free(top);
}
/* restore remaining (-D or predefined) symbols */
/* restore remaining (-D or predefined) symbols if they were
#undef'd in the file */
while (b) {
int v = b->v;
if (v >= TOK_IDENT && v < tok_ident) {
@ -3468,24 +3469,23 @@ ST_INLN void unget_tok(int last_tok)
ST_FUNC void preprocess_start(TCCState *s1)
{
char *buf;
s1->include_stack_ptr = s1->include_stack;
/* XXX: move that before to avoid having to initialize
file->ifdef_stack_ptr ? */
s1->ifdef_stack_ptr = s1->ifdef_stack;
file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
pp_once++;
pvtop = vtop = vstack - 1;
s1->pack_stack[0] = 0;
s1->pack_stack_ptr = s1->pack_stack;
set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
set_idnum('.', (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
buf = tcc_malloc(3 + strlen(file->filename));
sprintf(buf, "\"%s\"", file->filename);
tcc_undefine_symbol(s1, "__BASE_FILE__");
tcc_define_symbol(s1, "__BASE_FILE__", buf);
tcc_free(buf);
if (s1->nb_cmd_include_files) {
CString cstr;
int i;
@ -3558,6 +3558,9 @@ ST_FUNC void tccpp_delete(TCCState *s)
end_macro();
macro_ptr = NULL;
while (file)
tcc_close();
/* free tokens */
n = tok_ident - TOK_IDENT;
for(i = 0; i < n; i++)
@ -3712,7 +3715,9 @@ ST_FUNC int tcc_preprocess(TCCState *s1)
const char *p;
Sym *define_start;
define_start = define_stack;
preprocess_start(s1);
ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_PREPROCESS
@ -3721,7 +3726,6 @@ ST_FUNC int tcc_preprocess(TCCState *s1)
| PARSE_FLAG_SPACES
| PARSE_FLAG_ACCEPT_STRAYS
;
define_start = define_stack;
/* Credits to Fabrice Bellard's initial revision to demonstrate its
capability to compile and run itself, provided all numbers are