From c268c10f8b8853249b4754738e5d5e36e5cf205a Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Tue, 11 Mar 2014 20:16:44 +0100 Subject: [PATCH] Fix warning from clang --analyze --- include/simpleswitcher.h | 2 +- source/run-dialog.c | 8 +++++--- source/simpleswitcher.c | 17 +++++++++++++---- source/ssh-dialog.c | 8 +++++--- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/simpleswitcher.h b/include/simpleswitcher.h index 1947804b..74d4fe7d 100644 --- a/include/simpleswitcher.h +++ b/include/simpleswitcher.h @@ -47,7 +47,7 @@ MenuReturn menu( char **lines, char **input, char *prompt, /** * Allocator wrappers */ -void* allocate( unsigned long bytes ); +void* allocate( unsigned long bytes ) __attribute__((malloc)); void* allocate_clear( unsigned long bytes ); void* reallocate( void *ptr, unsigned long bytes ); diff --git a/source/run-dialog.c b/source/run-dialog.c index 9dab0c7e..2f76954d 100644 --- a/source/run-dialog.c +++ b/source/run-dialog.c @@ -221,7 +221,7 @@ static int sort_func ( const void *a, const void *b ) } static char ** get_apps ( ) { - int num_favorites = 0; + unsigned int num_favorites = 0; unsigned int index = 0; char *path; char **retv = NULL; @@ -277,7 +277,7 @@ static char ** get_apps ( ) // This is a nice little penalty, but doable? time will tell. // given num_favorites is max 25. - for ( int j = 0; found == 0 && j < num_favorites; j++ ) { + for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) { if ( strcasecmp( dent->d_name, retv[j] ) == 0 ) found = 1; } @@ -294,7 +294,9 @@ static char ** get_apps ( ) } // TODO: check this is still fast enough. (takes 1ms on laptop.) - qsort( &retv[num_favorites],index-num_favorites, sizeof( char* ), sort_func ); + if(index > num_favorites) { + qsort( &retv[num_favorites],index-num_favorites, sizeof( char* ), sort_func ); + } free( path ); #ifdef TIMING clock_gettime( CLOCK_REALTIME, &stop ); diff --git a/source/simpleswitcher.c b/source/simpleswitcher.c index 4dc390fd..ca80769a 100644 --- a/source/simpleswitcher.c +++ b/source/simpleswitcher.c @@ -340,12 +340,15 @@ winlist* winlist_new() l->data = allocate( sizeof( void* ) * ( WINLIST+1 ) ); return l; } -int winlist_append( winlist *l, Window w, void *d ) +int winlist_append( winlist *l , Window w, void *d ) { if ( l->len > 0 && !( l->len % WINLIST ) ) { l->array = reallocate( l->array, sizeof( Window ) * ( l->len+WINLIST+1 ) ); l->data = reallocate( l->data, sizeof( void* ) * ( l->len+WINLIST+1 ) ); } + // Make clang-check happy. + // TODO: make clang-check clear this should never be 0. + if(l->data == NULL || l->array == NULL) return 0; l->data[l->len] = d; l->array[l->len++] = w; @@ -503,10 +506,16 @@ char* window_get_text_prop( Window w, Atom atom ) if ( XGetTextProperty( display, w, &prop, atom ) && prop.value && prop.nitems ) { if ( prop.encoding == XA_STRING ) { res = allocate( strlen( ( char* )prop.value )+1 ); - strcpy( res, ( char* )prop.value ); + // make clang-check happy. + if(res) { + strcpy( res, ( char* )prop.value ); + } } else if ( Xutf8TextPropertyToTextList( display, &prop, &list, &count ) >= Success && count > 0 && *list ) { res = allocate( strlen( *list )+1 ); - strcpy( res, *list ); + // make clang-check happy. + if(res) { + strcpy( res, *list ); + } XFreeStringList( list ); } } @@ -732,7 +741,7 @@ static int calculate_common_prefix( char **filtered, int max_lines ) { int length_prefix = 0; - if ( filtered[0] != NULL ) { + if ( filtered && filtered[0] != NULL ) { int found = 1; char *p = filtered[0]; diff --git a/source/ssh-dialog.c b/source/ssh-dialog.c index 87f86bb9..bc1af0a8 100644 --- a/source/ssh-dialog.c +++ b/source/ssh-dialog.c @@ -193,7 +193,7 @@ static int sort_func ( const void *a, const void *b ) } static char ** get_ssh ( ) { - int num_favorites = 0; + unsigned int num_favorites = 0; unsigned int index = 0; char *path; char **retv = NULL; @@ -246,7 +246,7 @@ static char ** get_ssh ( ) // This is a nice little penalty, but doable? time will tell. // given num_favorites is max 25. - for ( int j = 0; found == 0 && j < num_favorites; j++ ) { + for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) { if ( strncasecmp( &buffer[start], retv[j],stop-start ) == 0 ) found = 1; } @@ -263,7 +263,9 @@ static char ** get_ssh ( ) } // TODO: check this is still fast enough. (takes 1ms on laptop.) - qsort( &retv[num_favorites],index-num_favorites, sizeof( char* ), sort_func ); + if(index > num_favorites) { + qsort( &retv[num_favorites],index-num_favorites, sizeof( char* ), sort_func ); + } free( path ); #ifdef TIMING clock_gettime( CLOCK_REALTIME, &stop );