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; break;
case 'p': case 'p':
al_push( &cmd, unescape( woptarg, 1));
break;
case 'c': 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; break;
}
case 'd': case 'd':
desc = woptarg; 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 ); // debug( 1, L"%ls -> %ls ->%ls", path, tilde, unescaped );
halloc_register( context, unescaped ); halloc_register( context, unescaped );
for( in = out = unescaped; *in; in++ ) for( in = out = unescaped; *in; in++ )
{
switch( *in )
{ {
case PROCESS_EXPAND: switch( *in )
case VARIABLE_EXPAND:
case VARIABLE_EXPAND_SINGLE:
case BRACKET_BEGIN:
case BRACKET_END:
case BRACKET_SEP:
{ {
has_magic = 1; case PROCESS_EXPAND:
break; case VARIABLE_EXPAND:
} case VARIABLE_EXPAND_SINGLE:
case BRACKET_BEGIN:
case INTERNAL_SEPARATOR: case BRACKET_END:
{ case BRACKET_SEP:
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 ) has_magic = 1;
{ break;
res = 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 ); 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 ); 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 ); unesc = unescape( arg_cpy, 1 );
free( arg_cpy ); if( !unesc )
/*
Check for invalid variable expansions
*/
for( pos = unesc; *pos; pos++ )
{ {
switch( *pos ) if( out )
{ {
case VARIABLE_EXPAND: error( SYNTAX_ERROR,
case VARIABLE_EXPAND_SINGLE: 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; case BRACKET_BEGIN:
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) )
{ {
err=1; err=1;
if( out ) if( out )
{ {
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
offset, offset,
COMPLETE_VAR_DESC, COMPLETE_VAR_BRACKET_DESC );
*(pos+1) );
print_errors( out, prefix); 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 ); free( unesc );
return err; return err;