Check the return value of the unescape call at all places

darcs-hash:20060615011154-ac50b-b9c8f31c39fda44080ef885764ce572b24866cd5.gz
This commit is contained in:
axel 2006-06-15 11:11:54 +10:00
parent 358f412abb
commit e9e0643817
3 changed files with 156 additions and 136 deletions

View file

@ -400,12 +400,20 @@ static int builtin_complete( wchar_t **argv )
break;
case 'p':
al_push( &cmd, unescape( woptarg, 1));
break;
case 'c':
al_push( &cmd, unescape( woptarg, 1) );
{
wchar_t *a = unescape( woptarg, 1);
if( a )
{
al_push( (opt=='p'?&path:&cmd), a );
}
else
{
sb_printf( sb_err, L"%ls: Invalid token '%ls'\n", argv[0], woptarg );
res = 1;
}
break;
}
case 'd':
desc = woptarg;

View file

@ -80,80 +80,80 @@ static int is_potential_path( const wchar_t *path )
// debug( 1, L"%ls -> %ls ->%ls", path, tilde, unescaped );
halloc_register( context, unescaped );
halloc_register( context, unescaped );
for( in = out = unescaped; *in; in++ )
{
switch( *in )
for( in = out = unescaped; *in; in++ )
{
case PROCESS_EXPAND:
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
case BRACKET_BEGIN:
case BRACKET_END:
case BRACKET_SEP:
switch( *in )
{
has_magic = 1;
break;
}
case INTERNAL_SEPARATOR:
{
break;
}
default:
{
*(out++) = *in;
break;
}
}
}
*out = 0;
if( !has_magic && wcslen( unescaped ) )
{
int must_be_dir = 0;
DIR *dir;
must_be_dir = unescaped[wcslen(unescaped)-1] == L'/';
if( must_be_dir )
{
dir = wopendir( unescaped );
res = !!dir;
closedir( dir );
}
else
{
wchar_t *dir_name, *base;
struct wdirent *ent;
dir_name = wdirname( halloc_wcsdup(context, unescaped) );
base = wbasename( halloc_wcsdup(context, unescaped) );
if( (wcscmp( dir_name, L"/" ) == 0 ) &&
(wcscmp( base, L"/" ) == 0 ) )
{
res = 1;
}
else if( (dir = wopendir( dir_name )) )
{
while( (ent = wreaddir( dir )) )
case PROCESS_EXPAND:
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
case BRACKET_BEGIN:
case BRACKET_END:
case BRACKET_SEP:
{
if( wcsncmp( ent->d_name, base, wcslen(base) ) == 0 )
{
res = 1;
break;
}
has_magic = 1;
break;
}
case INTERNAL_SEPARATOR:
{
break;
}
default:
{
*(out++) = *in;
break;
}
}
}
*out = 0;
if( !has_magic && wcslen( unescaped ) )
{
int must_be_dir = 0;
DIR *dir;
must_be_dir = unescaped[wcslen(unescaped)-1] == L'/';
if( must_be_dir )
{
dir = wopendir( unescaped );
res = !!dir;
closedir( dir );
}
else
{
wchar_t *dir_name, *base;
struct wdirent *ent;
dir_name = wdirname( halloc_wcsdup(context, unescaped) );
base = wbasename( halloc_wcsdup(context, unescaped) );
if( (wcscmp( dir_name, L"/" ) == 0 ) &&
(wcscmp( base, L"/" ) == 0 ) )
{
res = 1;
}
else if( (dir = wopendir( dir_name )) )
{
while( (ent = wreaddir( dir )) )
{
if( wcsncmp( ent->d_name, base, wcslen(base) ) == 0 )
{
res = 1;
break;
}
}
closedir( dir );
}
}
}
}
}
halloc_free( context );

144
parser.c
View file

@ -2721,91 +2721,103 @@ static int parser_test_argument( const wchar_t *arg, string_buffer_t *out, const
}
unesc = unescape( arg_cpy, 1 );
free( arg_cpy );
/*
Check for invalid variable expansions
*/
for( pos = unesc; *pos; pos++ )
if( !unesc )
{
switch( *pos )
if( out )
{
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
error( SYNTAX_ERROR,
offset,
L"Invalid token '%ls'", arg_cpy );
print_errors( out, prefix);
}
return 1;
}
else
{
/*
Check for invalid variable expansions
*/
for( pos = unesc; *pos; pos++ )
{
switch( *pos )
{
switch( *(pos+1))
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
{
case BRACKET_BEGIN:
switch( *(pos+1))
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_BRACKET_DESC );
print_errors( out, prefix);
}
break;
}
case INTERNAL_SEPARATOR:
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_PARAN_DESC );
print_errors( out, prefix);
}
break;
}
case 0:
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_NULL_DESC );
print_errors( out, prefix);
}
break;
}
default:
{
wchar_t n = *(pos+1);
if( n != VARIABLE_EXPAND &&
n != VARIABLE_EXPAND_SINGLE &&
!wcsvarchr(n) )
case BRACKET_BEGIN:
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_DESC,
*(pos+1) );
COMPLETE_VAR_BRACKET_DESC );
print_errors( out, prefix);
}
break;
}
case INTERNAL_SEPARATOR:
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_PARAN_DESC );
print_errors( out, prefix);
}
break;
}
case 0:
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_NULL_DESC );
print_errors( out, prefix);
}
break;
}
default:
{
wchar_t n = *(pos+1);
break;
}
if( n != VARIABLE_EXPAND &&
n != VARIABLE_EXPAND_SINGLE &&
!wcsvarchr(n) )
{
err=1;
if( out )
{
error( SYNTAX_ERROR,
offset,
COMPLETE_VAR_DESC,
*(pos+1) );
print_errors( out, prefix);
}
}
break;
}
}
}
break;
}
break;
}
}
}
}
free( arg_cpy );
free( unesc );
return err;