diff options
author | Qball Cow <qball@gmpclient.org> | 2014-05-26 09:19:58 +0200 |
---|---|---|
committer | Qball Cow <qball@gmpclient.org> | 2014-05-26 09:19:58 +0200 |
commit | c400c44ec1a3b3b7f7d85afc2878fd978d746afe (patch) | |
tree | 1b511154f3fb7f661354ba521def410414f64983 /source/history.c | |
parent | 469b566614f75f44c2f4840800f76347146ef945 (diff) |
Make cppcheck happy. (normally we assume malloc does not fail.)
Diffstat (limited to 'source/history.c')
-rw-r--r-- | source/history.c | 44 |
1 files changed, 26 insertions, 18 deletions
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 ); } |