From b70092e28140dc1725f568d0522e0a26507a0964 Mon Sep 17 00:00:00 2001 From: axel Date: Tue, 9 Jan 2007 13:20:05 +1000 Subject: [PATCH] Check for errors during string to integer conversion in various places darcs-hash:20070109032005-ac50b-29514c9c8c19c70b7cfe7670a5c74899f316931f.gz --- builtin_set.c | 9 +++++++-- expand.c | 8 ++++++-- history.c | 10 +++++++--- signal.c | 2 +- util.c | 20 +++++++++++++++++--- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/builtin_set.c b/builtin_set.c index 88e8fef..084031c 100644 --- a/builtin_set.c +++ b/builtin_set.c @@ -232,9 +232,14 @@ static int parse_index( array_list_t *indexes, while (*src != L']') { wchar_t *end; - long l_ind = wcstol(src, &end, 10); - if (end == src) + long l_ind; + + errno = 0; + + l_ind = wcstol(src, &end, 10); + + if( end==src || errno ) { sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src); return 0; diff --git a/expand.c b/expand.c index a49b533..f0a973f 100644 --- a/expand.c +++ b/expand.c @@ -387,8 +387,12 @@ static int find_process( const wchar_t *proc, else { - int jid = wcstol( proc, 0, 10 ); - if( jid > 0 ) + int jid; + wchar_t *end; + + errno = 0; + jid = wcstol( proc, &end, 10 ); + if( jid > 0 && !errno && !*end ) { j = job_get( jid ); if( (j != 0) && (j->command != 0 ) ) diff --git a/history.c b/history.c index 36cd54c..33b7f83 100644 --- a/history.c +++ b/history.c @@ -345,9 +345,13 @@ static item_t *item_get( history_mode_t *m, void *d ) if( *time_string ) { - time_t tm = (time_t)wcstol( time_string, 0, 10 ); - - if( tm && !errno ) + time_t tm; + wchar_t *end; + + errno = 0; + tm = (time_t)wcstol( time_string, &end, 10 ); + + if( tm && !errno && !*end ) { narrow_item.timestamp = tm; } diff --git a/signal.c b/signal.c index e18c5cd..3109e3e 100644 --- a/signal.c +++ b/signal.c @@ -385,7 +385,7 @@ int wcs2sig( const wchar_t *str ) } errno=0; res = wcstol( str, &end, 10 ); - if( !errno && end && !*end ) + if( !errno && res>=0 && !*end ) return res; return -1; diff --git a/util.c b/util.c index f6de95c..e709ce4 100644 --- a/util.c +++ b/util.c @@ -1112,9 +1112,23 @@ int wcsfilecmp( const wchar_t *a, const wchar_t *b ) if( iswdigit( *a ) && iswdigit( *b ) ) { wchar_t *aend, *bend; - long al = wcstol( a, &aend, 10 ); - long bl = wcstol( b, &bend, 10 ); - int diff = al - bl; + long al; + long bl; + int diff; + + errno = 0; + al = wcstol( a, &aend, 10 ); + bl = wcstol( b, &bend, 10 ); + + if( errno ) + { + /* + Huuuuuuuuge numbers - fall back to regular string comparison + */ + return wcscmp( a, b ); + } + + diff = al - bl; if( diff ) return diff>0?2:-2;