Skip to content

Commit

Permalink
Fix: bailing when error file could not be opened causes memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
positively-charged committed Nov 1, 2016
1 parent 8ffe426 commit 3e0a57f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
24 changes: 19 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static bool read_options( struct options*, char** );
static void strip_rslash( char* );
static bool source_object_files_same( struct options* );
static void print_usage( char* );
static bool perform_action( struct options* options, jmp_buf* root_bail );
static void perform_task( struct task* task );
static void perform_selected_task( struct task* task, struct cache* cache );
static void print_cache( struct task* task, struct cache* cache );
Expand Down Expand Up @@ -87,11 +88,9 @@ int main( int argc, char* argv[] ) {
}
jmp_buf bail;
if ( setjmp( bail ) == 0 ) {
struct task task;
t_init( &task, &options, &bail );
perform_task( &task );
t_deinit( &task );
result = EXIT_SUCCESS;
if ( perform_action( &options, &bail ) ) {
result = EXIT_SUCCESS;
}
}
deinit_object_file:
str_deinit( &compiler_dir );
Expand Down Expand Up @@ -354,6 +353,21 @@ void print_usage( char* path ) {
path );
}

bool perform_action( struct options* options, jmp_buf* root_bail ) {
bool success = false;
struct task task;
t_init( &task, options, root_bail );
jmp_buf bail;
if ( setjmp( bail ) == 0 ) {
task.bail = &bail;
perform_task( &task );
success = true;
}
task.bail = root_bail;
t_deinit( &task );
return success;
}

void perform_task( struct task* task ) {
if ( task->options->cache.enable ) {
struct cache cache;
Expand Down
49 changes: 17 additions & 32 deletions src/parse/phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "phase.h"
#include "cache/cache.h"

static void run_phase( struct parse* parse );

void p_init( struct parse* parse, struct task* task, struct cache* cache ) {
parse->task = task;
// NOTE: parse->queue not initialized.
Expand Down Expand Up @@ -42,6 +44,21 @@ void p_init( struct parse* parse, struct task* task, struct cache* cache ) {
}

void p_run( struct parse* parse ) {
bool success = false;
jmp_buf bail, *prev_bail = parse->task->bail;
if ( setjmp( bail ) == 0 ) {
parse->task->bail = &bail;
run_phase( parse );
success = true;
}
parse->task->bail = prev_bail;
p_deinit_tk( parse );
if ( ! success ) {
t_bail( parse->task );
}
}

void run_phase( struct parse* parse ) {
struct library* lib = t_add_library( parse->task );
lib->lang = ( parse->task->options->lang_specified ) ?
parse->task->options->lang : p_determine_lang_from_file_path(
Expand All @@ -65,37 +82,6 @@ void p_run( struct parse* parse ) {
p_read_tk( parse );
p_read_target_lib( parse );
}

/*
while ( parse->tk != TK_END ) {
printf( "a %s %d\n", parse->tk_text, parse->tk_length );
p_read_tk( parse );
}
p_bail( parse );
*/
/*
struct tkque_iter iter;
p_examine_token_queue( parse, &iter );
p_next_tk( parse, &iter );
p_next_tk( parse, &iter );
p_next_tk( parse, &iter );
p_next_tk( parse, &iter );
p_next_tk( parse, &iter );
p_next_tk( parse, &iter );
p_next_tk( parse, &iter );
printf( "%s\n", parse->token->text );
printf( "%s == %d\n", iter.token->text, iter.token->type );
while ( parse->tk != TK_END ) {
printf( "a %s %d\n", parse->tk_text, parse->tk_length );
p_read_tk( parse );
}
p_bail( parse );
return;
*/
//printf( "%s\n", iter.token->text );
//p_next_tk( parse, &iter );

// alloc_string_indexes( parse );
}

int p_determine_lang_from_file_path( const char* path ) {
Expand Down Expand Up @@ -143,6 +129,5 @@ void p_unexpect_last_name( struct parse* parse, struct pos* pos,
}

void p_bail( struct parse* parse ) {
p_deinit_tk( parse );
t_bail( parse->task );
}
1 change: 0 additions & 1 deletion src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ const char* t_decode_pos_file( struct task* task, struct pos* pos ) {
}

void t_bail( struct task* task ) {
t_deinit( task );
longjmp( *task->bail, 1 );
}

Expand Down

0 comments on commit 3e0a57f

Please sign in to comment.