From 5fe7c065dc6cd7c3e9afd9ce5fade31ff7d7b198 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 21 Feb 2012 19:33:11 -0800 Subject: [PATCH] Squash a bunch of leaks --- builtin_complete.cpp | 10 ++++------ exec.cpp | 36 ++++++++++++++++-------------------- history.cpp | 1 + io.cpp | 2 +- io.h | 9 +++++---- parser.cpp | 8 ++++---- 6 files changed, 31 insertions(+), 35 deletions(-) diff --git a/builtin_complete.cpp b/builtin_complete.cpp index 0e6afc457..24175c96a 100644 --- a/builtin_complete.cpp +++ b/builtin_complete.cpp @@ -292,7 +292,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv ) int authoritative = -1; int flags = COMPLETE_AUTO_SPACE; - string_buffer_t short_opt; + wcstring short_opt; wcstring_list_t gnu_opt, old_opt; const wchar_t *comp=L"", *desc=L"", *condition=L""; @@ -303,8 +303,6 @@ static int builtin_complete( parser_t &parser, wchar_t **argv ) static int recursion_level=0; - sb_init( &short_opt ); - argc = builtin_count_args( argv ); woptind=0; @@ -455,7 +453,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv ) break; case 's': - sb_append( &short_opt, woptarg ); + short_opt.append(woptarg); break; case 'l': @@ -604,7 +602,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv ) { builtin_complete_remove( cmd, path, - (wchar_t *)short_opt.buff, + short_opt.c_str(), gnu_opt, old_opt ); } @@ -612,7 +610,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv ) { builtin_complete_add( cmd, path, - (wchar_t *)short_opt.buff, + short_opt.c_str(), gnu_opt, old_opt, result_mode, diff --git a/exec.cpp b/exec.cpp index 38202925c..d4954388e 100644 --- a/exec.cpp +++ b/exec.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef HAVE_SIGINFO_H #include @@ -286,7 +287,7 @@ static int handle_child_io( io_data_t *io ) case IO_FILE: { - if( (tmp=wopen( io->param1.filename, + if( (tmp=wopen( io->filename, io->param2.flags, OPEN_MASK ) )==-1 ) { if( ( io->param2.flags & O_EXCL ) && @@ -294,13 +295,13 @@ static int handle_child_io( io_data_t *io ) { debug( 1, NOCLOB_ERROR, - io->param1.filename ); + io->filename.c_str() ); } else { debug( 1, FILE_ERROR, - io->param1.filename ); + io->filename.c_str() ); wperror( L"open" ); } @@ -659,7 +660,7 @@ static void io_untransmogrify( io_data_t * in, io_data_t *out ) exec_close( out->param1.old_fd ); break; } - free(out); + delete out; } @@ -674,14 +675,10 @@ static void io_untransmogrify( io_data_t * in, io_data_t *out ) */ static io_data_t *io_transmogrify( io_data_t * in ) { - io_data_t *out; - if( !in ) return 0; - out = (io_data_t *)malloc( sizeof( io_data_t ) ); - if( !out ) - DIE_MEM(); + std::auto_ptr out(new io_data_t()); out->fd = in->fd; out->io_mode = IO_FD; @@ -698,7 +695,7 @@ static io_data_t *io_transmogrify( io_data_t * in ) case IO_BUFFER: case IO_PIPE: { - memcpy( out, in, sizeof(io_data_t)); + *out = *in; break; } @@ -709,15 +706,14 @@ static io_data_t *io_transmogrify( io_data_t * in ) { int fd; - if( (fd=wopen( in->param1.filename, in->param2.flags, OPEN_MASK ) )==-1 ) + if( (fd=wopen( in->filename, in->param2.flags, OPEN_MASK ) )==-1 ) { debug( 1, FILE_ERROR, - in->param1.filename ); + in->filename.c_str() ); wperror( L"open" ); - free( out ); - return 0; + return NULL; } out->param1.old_fd = fd; @@ -730,12 +726,12 @@ static io_data_t *io_transmogrify( io_data_t * in ) out->next = io_transmogrify( in->next ); if( !out->next ) { - io_untransmogrify( in, out ); - return 0; + io_untransmogrify( in, out.release() ); + return NULL; } } - return out; + return out.release(); } /** @@ -1246,13 +1242,13 @@ void exec( parser_t &parser, job_t *j ) case IO_FILE: { - builtin_stdin=wopen( in->param1.filename, + builtin_stdin=wopen( in->filename, in->param2.flags, OPEN_MASK ); if( builtin_stdin == -1 ) { debug( 1, FILE_ERROR, - in->param1.filename ); + in->filename.c_str() ); wperror( L"open" ); } else @@ -1530,7 +1526,7 @@ void exec( parser_t &parser, job_t *j ) for( io = j->io; io; io=io->next ) { - if( io->io_mode == IO_FILE && wcscmp(io->param1.filename, L"/dev/null" )) + if( io->io_mode == IO_FILE && io->filename != L"/dev/null") { skip_fork = 0; } diff --git a/history.cpp b/history.cpp index f61f47fcf..74363a923 100644 --- a/history.cpp +++ b/history.cpp @@ -765,6 +765,7 @@ void history_t::add_with_file_detection(const wcstring &str) } } } + tok_destroy(&tokenizer); if (! potential_paths.empty()) { /* We have some paths. Make a context. */ diff --git a/io.cpp b/io.cpp index 3b30b2ef3..2c18de961 100644 --- a/io.cpp +++ b/io.cpp @@ -100,7 +100,7 @@ void io_buffer_read( io_data_t *d ) io_data_t *io_buffer_create( int is_input ) { - io_data_t *buffer_redirect = (io_data_t *)malloc( sizeof( io_data_t )); + io_data_t *buffer_redirect = new io_data_t(); buffer_redirect->io_mode=IO_BUFFER; buffer_redirect->next=0; diff --git a/io.h b/io.h index 56eba9c08..ee9e2242f 100644 --- a/io.h +++ b/io.h @@ -24,12 +24,13 @@ struct io_data_t { /** Fds for IO_PIPE and for IO_BUFFER */ int pipe_fd[2]; - /** Filename IO_FILE */ - wchar_t *filename; /** fd to redirect specified fd to, for IO_FD*/ int old_fd; - } param1 - ; + } param1; + + /** Filename IO_FILE */ + wcstring filename; + /** Second type-specific paramter for redirection */ diff --git a/parser.cpp b/parser.cpp index 54e52ab5f..915210242 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1486,25 +1486,25 @@ void parser_t::parse_job_argument_list( process_t *p, case TOK_REDIRECT_APPEND: new_io->io_mode = IO_FILE; new_io->param2.flags = O_CREAT | O_APPEND | O_WRONLY; - new_io->param1.filename = wcsdup(target.c_str()); // PCA LEAKS! + new_io->filename = target; break; case TOK_REDIRECT_OUT: new_io->io_mode = IO_FILE; new_io->param2.flags = O_CREAT | O_WRONLY | O_TRUNC; - new_io->param1.filename = wcsdup(target.c_str()); // PCA LEAKS! + new_io->filename = target; break; case TOK_REDIRECT_NOCLOB: new_io->io_mode = IO_FILE; new_io->param2.flags = O_CREAT | O_EXCL | O_WRONLY; - new_io->param1.filename = wcsdup(target.c_str()); // PCA LEAKS! + new_io->filename = target; break; case TOK_REDIRECT_IN: new_io->io_mode = IO_FILE; new_io->param2.flags = O_RDONLY; - new_io->param1.filename = wcsdup(target.c_str()); // PCA LEAKS! + new_io->filename = target; break; case TOK_REDIRECT_FD: