From c400c44ec1a3b3b7f7d85afc2878fd978d746afe Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Mon, 26 May 2014 09:19:58 +0200 Subject: [PATCH] Make cppcheck happy. (normally we assume malloc does not fail.) --- source/dmenu-dialog.c | 7 ++++++- source/history.c | 44 +++++++++++++++++++++++++------------------ source/rofi.c | 14 +++++++++----- source/run-dialog.c | 12 ++++++++---- source/ssh-dialog.c | 12 ++++++++---- 5 files changed, 57 insertions(+), 32 deletions(-) diff --git a/source/dmenu-dialog.c b/source/dmenu-dialog.c index 0e38f963..711431f7 100644 --- a/source/dmenu-dialog.c +++ b/source/dmenu-dialog.c @@ -46,7 +46,12 @@ static char **get_dmenu ( void ) while ( fgets ( buffer, 1024, stdin ) != NULL ) { - retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); + char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); + if ( tr == NULL ) + { + return retv; + } + retv = tr; retv[index] = strdup ( buffer ); retv[index + 1] = NULL; diff --git a/source/history.c b/source/history.c index 67aa8f4b..6c0f209b 100644 --- a/source/history.c +++ b/source/history.c @@ -94,7 +94,14 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length ) { continue; } - retv = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) ); + // Resize and check. + _element **tr = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) ); + if ( tr == NULL ) + { + return retv; + } + retv = tr; + retv[( *length )] = malloc ( sizeof ( _element ) ); // remove trailing \n buffer[strlen ( buffer ) - 1] = '\0'; @@ -146,16 +153,23 @@ void history_set ( const char *filename, const char *entry ) { // If not exists, add it. // Increase list by one - list = realloc ( list, ( length + 2 ) * sizeof ( _element * ) ); - list[length] = malloc ( sizeof ( _element ) ); - // Copy name - strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH ); - list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0'; - // set # hits - list[length]->index = 1; + _element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) ); + if ( tr != NULL ) + { + list = tr; + list[length] = malloc ( sizeof ( _element ) ); + // Copy name + if ( list[length] != NULL ) + { + strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH ); + list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0'; + // set # hits + list[length]->index = 1; - length++; - list[length] = NULL; + length++; + list[length] = NULL; + } + } } // Rewind. @@ -173,15 +187,9 @@ void history_set ( const char *filename, const char *entry ) // Free the list. for ( unsigned int iter = 0; iter < length; iter++ ) { - if ( list[iter] != NULL ) - { - free ( list[iter] ); - } - } - if ( list != NULL ) - { - free ( list ); + free ( list[iter] ); } + free ( list ); // Close file. fclose ( fd ); } diff --git a/source/rofi.c b/source/rofi.c index b12df3f7..52e3d9b4 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -136,10 +136,14 @@ static char **tokenize ( const char *input ) token != NULL; token = strtok_r ( NULL, " ", &saveptr ) ) { - retv = realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) ); - retv[num_tokens + 1] = NULL; - retv[num_tokens] = token; - num_tokens++; + char **tr = realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) ); + if ( tr != NULL ) + { + retv = tr; + retv[num_tokens + 1] = NULL; + retv[num_tokens] = token; + num_tokens++; + } } return retv; @@ -608,7 +612,7 @@ void monitor_dimensions ( Screen *screen, int x, int y, workarea *mon ) mon->w = WidthOfScreen ( screen ); mon->h = HeightOfScreen ( screen ); -// locate the current monitor + // locate the current monitor if ( XineramaIsActive ( display ) ) { int monitors; diff --git a/source/run-dialog.c b/source/run-dialog.c index 1064ecdb..de05f0d6 100644 --- a/source/run-dialog.c +++ b/source/run-dialog.c @@ -178,10 +178,14 @@ static char ** get_apps ( void ) continue; } - retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); - retv[index] = strdup ( dent->d_name ); - retv[index + 1] = NULL; - index++; + char ** tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); + if ( tr != NULL ) + { + retv = tr; + retv[index] = strdup ( dent->d_name ); + retv[index + 1] = NULL; + index++; + } } closedir ( dir ); diff --git a/source/ssh-dialog.c b/source/ssh-dialog.c index 5670325f..f16b40a3 100644 --- a/source/ssh-dialog.c +++ b/source/ssh-dialog.c @@ -206,10 +206,14 @@ static char ** get_ssh ( void ) continue; } - retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); - retv[index] = strndup ( &buffer[start], stop - start ); - retv[index + 1] = NULL; - index++; + char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); + if ( tr != NULL ) + { + retv = tr; + retv[index] = strndup ( &buffer[start], stop - start ); + retv[index + 1] = NULL; + index++; + } } }