From b37eeb92ef73f68b51178ca17398d8b32d440da4 Mon Sep 17 00:00:00 2001 From: axel Date: Mon, 26 Dec 2005 08:00:44 +1000 Subject: [PATCH] Fisx crash buh when completing string ending in backslash darcs-hash:20051225220044-ac50b-b880ffe649d04ed1e5ec6786d0b59eed8068d182.gz --- common.c | 1 - complete.c | 5 +++-- expand.c | 7 +++++++ reader.c | 32 +++++++++++++++++++++++++++----- tokenizer.c | 19 +++++++------------ wildcard.c | 7 +++++-- 6 files changed, 49 insertions(+), 22 deletions(-) diff --git a/common.c b/common.c index 7fc868a0f..76bc2029c 100644 --- a/common.c +++ b/common.c @@ -932,7 +932,6 @@ wchar_t *escape( const wchar_t *in, case L'^': case L'<': case L'>': - case L'@': case L'(': case L')': case L'[': diff --git a/complete.c b/complete.c index e8fe8d5b6..7280c04b6 100644 --- a/complete.c +++ b/complete.c @@ -2017,7 +2017,7 @@ void complete( const wchar_t *cmd, tok_init( &tok, buff, TOK_ACCEPT_UNFINISHED ); - while( !end_loop ) + while( tok_has_next( &tok) && !end_loop ) { switch( tok_last_type( &tok ) ) { @@ -2063,7 +2063,8 @@ void complete( const wchar_t *cmd, reader_current_token_extent( &begin, &end, &prev_begin, &prev_end ); current_token = wcsndup( begin, reader_get_cursor_pos()-(begin-reader_get_buffer()) ); - prev_token = wcsndup( prev_begin, prev_end - prev_begin ); + + prev_token = prev_begin ? wcsndup( prev_begin, prev_end - prev_begin ): wcsdup(L""); // fwprintf( stderr, L"on_command: %d, %ls %ls\n", on_command, current_compmand, current_token ); diff --git a/expand.c b/expand.c index 220d985a3..ff417ea51 100644 --- a/expand.c +++ b/expand.c @@ -20,6 +20,8 @@ parameter expansion. #include #include +#include + #ifdef SunOS #include #endif @@ -1174,6 +1176,11 @@ int expand_locate_subshell( wchar_t *in, *begin = paran_begin; *end = paran_count?in+wcslen(in):paran_end; +/* assert( *begin >= in ); + assert( *begin < (in+wcslen(in) ) ); + assert( *end >= *begin ); + assert( *end < (in+wcslen(in) ) ); +*/ return 1; } diff --git a/reader.c b/reader.c index f61c2599a..7f8e929ad 100644 --- a/reader.c +++ b/reader.c @@ -52,6 +52,8 @@ commence. #include #include +#include + #include "util.h" #include "wutil.h" #include "highlight.h" @@ -1829,8 +1831,6 @@ void reader_current_token_extent( wchar_t **tok_begin, reader_current_subshell_extent( &begin, &end ); -// fwprintf( stderr, L"Lalala: %d %d %d\n", begin-data->buff, end-data->buff, pos ); - if( !end || !begin ) return; @@ -1841,6 +1841,11 @@ void reader_current_token_extent( wchar_t **tok_begin, pa = data->buff + pos; pb = pa; + assert( begin >= data->buff ); + assert( begin <= (data->buff+wcslen(data->buff) ) ); + assert( end >= begin ); + assert( end <= (data->buff+wcslen(data->buff) ) ); + buffcpy = wcsndup( begin, end-begin ); if( !buffcpy ) @@ -1855,15 +1860,27 @@ void reader_current_token_extent( wchar_t **tok_begin, int tok_begin = tok_get_pos( &tok ); int tok_end=tok_begin; + /* + Calculate end of token + */ if( tok_last_type( &tok ) == TOK_STRING ) tok_end +=wcslen(tok_last(&tok)); - + + /* + Cursor was before beginning of this token, means that the + cursor is between two tokens, so we set it to a zero element + string and break + */ if( tok_begin > pos ) { a = b = data->buff + pos; break; } + /* + If cursor is inside the token, this is the token we are + looking for. If so, set a and b and break + */ if( tok_end >= pos ) { a = begin + tok_get_pos( &tok ); @@ -1874,6 +1891,9 @@ void reader_current_token_extent( wchar_t **tok_begin, break; } + /* + Remember previous string token + */ if( tok_last_type( &tok ) == TOK_STRING ) { pa = begin + tok_get_pos( &tok ); @@ -1881,7 +1901,6 @@ void reader_current_token_extent( wchar_t **tok_begin, } } -// fwprintf( stderr, L"Res: %d %d\n", *a-data->buff, *b-data->buff ); free( buffcpy); tok_destroy( &tok ); @@ -1895,7 +1914,10 @@ void reader_current_token_extent( wchar_t **tok_begin, if( prev_end ) *prev_end = pb; -// fwprintf( stderr, L"w00t\n" ); + assert( pa >= data->buff ); + assert( pa <= (data->buff+wcslen(data->buff) ) ); + assert( pb >= pa ); + assert( pb <= (data->buff+wcslen(data->buff) ) ); } diff --git a/tokenizer.c b/tokenizer.c index 50b6b8aca..ef9b0566d 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -119,12 +119,9 @@ void tok_init( tokenizer *tok, const wchar_t *b, int flags ) memset( tok, 0, sizeof( tokenizer) ); - tok ->last = 0; - tok ->last_len = 0; tok->accept_unfinished = flags & TOK_ACCEPT_UNFINISHED; tok->show_comments = flags & TOK_SHOW_COMMENTS; - tok->has_next=1; - + tok->has_next=1; /* Before we copy the buffer we need to check that it is not @@ -139,12 +136,6 @@ void tok_init( tokenizer *tok, const wchar_t *b, int flags ) tok->has_next = (*b != L'\0'); tok->orig_buff = tok->buff = (wchar_t *)(b); - - if( !tok->orig_buff ) - { - die_mem(); - - } if( tok->accept_unfinished ) { @@ -154,7 +145,11 @@ void tok_init( tokenizer *tok, const wchar_t *b, int flags ) if( tok->orig_buff[l-1] == L'\\' ) { tok->free_orig = 1; - tok->orig_buff = wcsdup( tok->orig_buff ); + tok->orig_buff = tok->buff = wcsdup( tok->orig_buff ); + if( !tok->orig_buff ) + { + die_mem(); + } tok->orig_buff[l-1] = L'\0'; } } @@ -624,7 +619,7 @@ wchar_t *tok_first( const wchar_t *str ) int tok_get_pos( tokenizer *tok ) { - return tok->last_pos; + return tok->last_pos + (tok->free_orig?1:0); } diff --git a/wildcard.c b/wildcard.c index 903864727..790591abd 100644 --- a/wildcard.c +++ b/wildcard.c @@ -317,8 +317,11 @@ static int test_flags( wchar_t *filename, return 1; struct stat buf; - wstat( filename, &buf ); - + if( wstat( filename, &buf ) == -1 ) + { + return 1; + } + if( S_IFDIR & buf.st_mode ) return 1;