From 3ad7de0fcb9ec08f09af3c86d1f094a534c399af Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 30 Jan 2012 11:15:06 -0800 Subject: [PATCH] Fix some leaks and clean up expand_pid to use wcstring --- exec.cpp | 12 ++++-------- expand.cpp | 31 +++++++++++-------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/exec.cpp b/exec.cpp index 031914d23..a25f6ce57 100644 --- a/exec.cpp +++ b/exec.cpp @@ -1175,7 +1175,6 @@ void exec( parser_t &parser, job_t *j ) { case INTERNAL_FUNCTION: { - const wchar_t * orig_def; wchar_t * def=0; int shadows; @@ -1187,23 +1186,19 @@ void exec( parser_t &parser, job_t *j ) */ signal_unblock(); - orig_def = function_get_definition( p->argv0() ); + const wchar_t * orig_def = function_get_definition( p->argv0() ); // function_get_named_arguments may trigger autoload, which deallocates the orig_def. // We should make function_get_definition return a wcstring (but how to handle NULL...) if (orig_def) - orig_def = wcsdup(orig_def); + def = wcsdup(orig_def); wcstring_list_t named_arguments = function_get_named_arguments( p->argv0() ); shadows = function_get_shadows( p->argv0() ); signal_block(); - if( orig_def ) - { - def = (wchar_t *)halloc_register( j, const_cast(orig_def) ); - } - if( def == 0 ) + if( def == NULL ) { debug( 0, _( L"Unknown function '%ls'" ), p->argv0() ); break; @@ -1235,6 +1230,7 @@ void exec( parser_t &parser, job_t *j ) parser.allow_function(); parser.pop_block(); + free(def); break; } diff --git a/expand.cpp b/expand.cpp index e31133474..8f400b57c 100644 --- a/expand.cpp +++ b/expand.cpp @@ -656,21 +656,20 @@ static int find_process( const wchar_t *proc, /** Process id expansion */ -static int expand_pid( wchar_t *in, +static int expand_pid( const wcstring &instr, int flags, std::vector &out ) { - - CHECK( in, 0 ); -// CHECK( out, 0 ); - - if( *in != PROCESS_EXPAND ) + + if( instr.empty() || instr.at(0) != PROCESS_EXPAND ) { completion_t data_to_push; - data_to_push.completion = in; + data_to_push.completion = instr; out.push_back( data_to_push ); return 1; } + + const wchar_t * const in = instr.c_str(); if( flags & ACCEPT_INCOMPLETE ) { @@ -693,8 +692,7 @@ static int expand_pid( wchar_t *in, { if( wcscmp( (in+1), SELF_STR )==0 ) { - wchar_t *str= (wchar_t *)malloc( sizeof(wchar_t)*32); - free(in); + wchar_t str[32]; swprintf( str, 32, L"%d", getpid() ); completion_t data_to_push; @@ -711,7 +709,6 @@ static int expand_pid( wchar_t *in, if( proc_last_bg_pid > 0 ) { str = (wchar_t *)malloc( sizeof(wchar_t)*32); - free(in); swprintf( str, 32, L"%d", proc_last_bg_pid ); completion_t data_to_push; data_to_push.completion = str; @@ -729,21 +726,15 @@ static int expand_pid( wchar_t *in, if( prev == out.size() ) { - if( flags & ACCEPT_INCOMPLETE ) - free( in ); - else + if( ! (flags & ACCEPT_INCOMPLETE) ) { - free(in); return 0; } } - else - { - free( in ); - } return 1; } + /* static int expand_pid2( const wcstring &in, int flags, std::vector &outputs ) { @@ -2174,7 +2165,7 @@ int expand_string2( const wcstring &input, std::vector &output, in interested in other completions, so we short-circut and return */ - expand_pid( wcsdup(next.c_str()), flags, output ); + expand_pid( next, flags, output ); return EXPAND_OK; } else @@ -2186,7 +2177,7 @@ int expand_string2( const wcstring &input, std::vector &output, in } else { - if( !expand_pid( wcsdup(next.c_str()), flags, *out ) ) + if( !expand_pid( next, flags, *out ) ) { return EXPAND_ERROR; }