Make cppcheck happy. (normally we assume malloc does not fail.)

This commit is contained in:
Qball Cow 2014-05-26 09:19:58 +02:00
parent 469b566614
commit c400c44ec1
5 changed files with 57 additions and 32 deletions

View File

@ -46,7 +46,12 @@ static char **get_dmenu ( void )
while ( fgets ( buffer, 1024, stdin ) != NULL ) 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] = strdup ( buffer );
retv[index + 1] = NULL; retv[index + 1] = NULL;

View File

@ -94,7 +94,14 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{ {
continue; 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 ) ); retv[( *length )] = malloc ( sizeof ( _element ) );
// remove trailing \n // remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0'; buffer[strlen ( buffer ) - 1] = '\0';
@ -146,9 +153,14 @@ void history_set ( const char *filename, const char *entry )
{ {
// If not exists, add it. // If not exists, add it.
// Increase list by one // Increase list by one
list = realloc ( list, ( length + 2 ) * sizeof ( _element * ) ); _element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
if ( tr != NULL )
{
list = tr;
list[length] = malloc ( sizeof ( _element ) ); list[length] = malloc ( sizeof ( _element ) );
// Copy name // Copy name
if ( list[length] != NULL )
{
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH ); strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0'; list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits // set # hits
@ -157,6 +169,8 @@ void history_set ( const char *filename, const char *entry )
length++; length++;
list[length] = NULL; list[length] = NULL;
} }
}
}
// Rewind. // Rewind.
fseek ( fd, 0L, SEEK_SET ); fseek ( fd, 0L, SEEK_SET );
@ -172,16 +186,10 @@ void history_set ( const char *filename, const char *entry )
} }
// Free the list. // Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) for ( unsigned int iter = 0; iter < length; iter++ )
{
if ( list[iter] != NULL )
{ {
free ( list[iter] ); free ( list[iter] );
} }
}
if ( list != NULL )
{
free ( list ); free ( list );
}
// Close file. // Close file.
fclose ( fd ); fclose ( fd );
} }

View File

@ -136,11 +136,15 @@ static char **tokenize ( const char *input )
token != NULL; token != NULL;
token = strtok_r ( NULL, " ", &saveptr ) ) token = strtok_r ( NULL, " ", &saveptr ) )
{ {
retv = realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) ); char **tr = realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) );
if ( tr != NULL )
{
retv = tr;
retv[num_tokens + 1] = NULL; retv[num_tokens + 1] = NULL;
retv[num_tokens] = token; retv[num_tokens] = token;
num_tokens++; num_tokens++;
} }
}
return retv; return retv;
} }
@ -608,7 +612,7 @@ void monitor_dimensions ( Screen *screen, int x, int y, workarea *mon )
mon->w = WidthOfScreen ( screen ); mon->w = WidthOfScreen ( screen );
mon->h = HeightOfScreen ( screen ); mon->h = HeightOfScreen ( screen );
// locate the current monitor // locate the current monitor
if ( XineramaIsActive ( display ) ) if ( XineramaIsActive ( display ) )
{ {
int monitors; int monitors;

View File

@ -178,11 +178,15 @@ static char ** get_apps ( void )
continue; continue;
} }
retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); char ** tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr != NULL )
{
retv = tr;
retv[index] = strdup ( dent->d_name ); retv[index] = strdup ( dent->d_name );
retv[index + 1] = NULL; retv[index + 1] = NULL;
index++; index++;
} }
}
closedir ( dir ); closedir ( dir );
} }

View File

@ -206,12 +206,16 @@ static char ** get_ssh ( void )
continue; continue;
} }
retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr != NULL )
{
retv = tr;
retv[index] = strndup ( &buffer[start], stop - start ); retv[index] = strndup ( &buffer[start], stop - start );
retv[index + 1] = NULL; retv[index + 1] = NULL;
index++; index++;
} }
} }
}
fclose ( fd ); fclose ( fd );
} }