A few more error checks during string to integer conversion in various places

darcs-hash:20070109134117-ac50b-8816e238b0b6aa3fe83c0a4695d719a60b0d17d4.gz
This commit is contained in:
axel 2007-01-09 23:41:17 +10:00
parent b70092e281
commit 6e71b5a59c
3 changed files with 122 additions and 90 deletions

8
main.c
View file

@ -195,8 +195,12 @@ static int fish_parse_opt( int argc, char **argv, char **cmd_ptr )
case 'd': case 'd':
{ {
char *end; char *end;
int tmp = strtol(optarg, &end, 10); int tmp;
if( tmp >= 0 && tmp <=10 && !*end )
errno = 0;
tmp = strtol(optarg, &end, 10);
if( tmp >= 0 && tmp <=10 && !*end && !errno )
{ {
debug_level=tmp; debug_level=tmp;
} }

199
parser.c
View file

@ -112,6 +112,11 @@ The fish parser. Contains functions for parsing and evaluating code.
*/ */
#define ILLEGAL_CMD_ERR_MSG _( L"Illegal command name '%ls'") #define ILLEGAL_CMD_ERR_MSG _( L"Illegal command name '%ls'")
/**
Error message when encountering an illegal file descriptor
*/
#define ILLEGAL_FD_ERR_MSG _( L"Illegal file descriptor '%ls'")
/** /**
Error message for wildcards with no matches Error message for wildcards with no matches
*/ */
@ -1366,6 +1371,8 @@ static void parse_job_argument_list( process_t *p,
{ {
case TOK_PIPE: case TOK_PIPE:
{ {
wchar_t *end;
if( (p->type == INTERNAL_EXEC) ) if( (p->type == INTERNAL_EXEC) )
{ {
error( SYNTAX_ERROR, error( SYNTAX_ERROR,
@ -1373,14 +1380,22 @@ static void parse_job_argument_list( process_t *p,
EXEC_ERR_MSG ); EXEC_ERR_MSG );
return; return;
} }
p->pipe_write_fd = wcstol( tok_last( tok ), 0, 10 );
errno = 0;
p->pipe_write_fd = wcstol( tok_last( tok ), &end, 10 );
if( p->pipe_write_fd < 0 || errno || *end )
{
error( SYNTAX_ERROR,
tok_get_pos( tok ),
ILLEGAL_FD_ERR_MSG,
tok_last( tok ) );
return;
}
if( !p->argv ) if( !p->argv )
halloc_register( j, p->argv = list_to_char_arr( args ) ); halloc_register( j, p->argv = list_to_char_arr( args ) );
p->next = halloc( j, sizeof( process_t ) ); p->next = halloc( j, sizeof( process_t ) );
if( p->next == 0 )
{
DIE_MEM();
}
tok_next( tok ); tok_next( tok );
/* /*
@ -1505,7 +1520,8 @@ static void parse_job_argument_list( process_t *p,
int type = tok_last_type( tok ); int type = tok_last_type( tok );
io_data_t *new_io; io_data_t *new_io;
wchar_t *target = 0; wchar_t *target = 0;
wchar_t *end;
/* /*
Don't check redirections in skipped part Don't check redirections in skipped part
@ -1530,93 +1546,104 @@ static void parse_job_argument_list( process_t *p,
if( !new_io ) if( !new_io )
DIE_MEM(); DIE_MEM();
errno = 0;
new_io->fd = wcstol( tok_last( tok ), new_io->fd = wcstol( tok_last( tok ),
0, &end,
10 ); 10 );
tok_next( tok ); if( new_io->fd < 0 || errno || *end )
switch( tok_last_type( tok ) )
{ {
case TOK_STRING: error( SYNTAX_ERROR,
{ tok_get_pos( tok ),
target = (wchar_t *)expand_one( j, wcsdup( tok_last( tok ) ), 0); ILLEGAL_FD_ERR_MSG,
tok_last( tok ) );
if( target == 0 && error_code == 0 )
{
error( SYNTAX_ERROR,
tok_get_pos( tok ),
REDIRECT_TOKEN_ERR_MSG,
tok_last( tok ) );
}
break;
}
default:
error( SYNTAX_ERROR,
tok_get_pos( tok ),
REDIRECT_TOKEN_ERR_MSG,
tok_get_desc( tok_last_type(tok)) );
}
if( target == 0 || wcslen( target )==0 )
{
if( error_code == 0 )
error( SYNTAX_ERROR,
tok_get_pos( tok ),
_(L"Invalid IO redirection") );
tok_next(tok);
} }
else else
{ {
tok_next( tok );
switch( tok_last_type( tok ) )
switch( type )
{ {
case TOK_REDIRECT_APPEND: case TOK_STRING:
new_io->io_mode = IO_FILE;
new_io->param2.flags = O_CREAT | O_APPEND | O_WRONLY;
new_io->param1.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 = target;
break;
case TOK_REDIRECT_IN:
new_io->io_mode = IO_FILE;
new_io->param2.flags = O_RDONLY;
new_io->param1.filename = target;
break;
case TOK_REDIRECT_FD:
{ {
if( wcscmp( target, L"-" ) == 0 ) target = (wchar_t *)expand_one( j, wcsdup( tok_last( tok ) ), 0);
{
new_io->io_mode = IO_CLOSE; if( target == 0 && error_code == 0 )
} {
else error( SYNTAX_ERROR,
{ tok_get_pos( tok ),
new_io->io_mode = IO_FD; REDIRECT_TOKEN_ERR_MSG,
new_io->param1.old_fd = wcstol( target, tok_last( tok ) );
0,
10 );
if( ( new_io->param1.old_fd < 0 ) ||
( new_io->param1.old_fd > 10 ) )
{
error( SYNTAX_ERROR,
tok_get_pos( tok ),
_(L"Requested redirection to something that is not a file descriptor %ls"),
target );
tok_next(tok);
}
} }
break; break;
} }
default:
error( SYNTAX_ERROR,
tok_get_pos( tok ),
REDIRECT_TOKEN_ERR_MSG,
tok_get_desc( tok_last_type(tok)) );
}
if( target == 0 || wcslen( target )==0 )
{
if( error_code == 0 )
error( SYNTAX_ERROR,
tok_get_pos( tok ),
_(L"Invalid IO redirection") );
tok_next(tok);
}
else
{
switch( type )
{
case TOK_REDIRECT_APPEND:
new_io->io_mode = IO_FILE;
new_io->param2.flags = O_CREAT | O_APPEND | O_WRONLY;
new_io->param1.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 = target;
break;
case TOK_REDIRECT_IN:
new_io->io_mode = IO_FILE;
new_io->param2.flags = O_RDONLY;
new_io->param1.filename = target;
break;
case TOK_REDIRECT_FD:
{
if( wcscmp( target, L"-" ) == 0 )
{
new_io->io_mode = IO_CLOSE;
}
else
{
new_io->io_mode = IO_FD;
new_io->param1.old_fd = wcstol( target,
0,
10 );
if( ( new_io->param1.old_fd < 0 ) ||
( new_io->param1.old_fd > 10 ) )
{
error( SYNTAX_ERROR,
tok_get_pos( tok ),
_(L"Requested redirection to something that is not a file descriptor %ls"),
target );
tok_next(tok);
}
}
break;
}
}
} }
} }
@ -1677,14 +1704,14 @@ static void parse_job_argument_list( process_t *p,
} }
/* /*
static void print_block_stack( block_t *b ) static void print_block_stack( block_t *b )
{ {
if( !b ) if( !b )
return; return;
print_block_stack( b->outer ); print_block_stack( b->outer );
debug( 0, L"Block type %ls, skip: %d", parser_get_block_desc( b->type ), b->skip ); debug( 0, L"Block type %ls, skip: %d", parser_get_block_desc( b->type ), b->skip );
} }
*/ */
/** /**

View file

@ -102,10 +102,11 @@ int translate_color( char *str )
if( !str ) if( !str )
return -1; return -1;
errno = 0;
color = strtol( str, &endptr, 10 ); color = strtol( str, &endptr, 10 );
if(endptr<=str)
if( *endptr || color<0 || errno )
{ {
int i; int i;
color = -1; color = -1;