wmail: handle allocation failures.

This commit is contained in:
Jeremy Sowden 2019-05-27 22:52:06 +01:00 committed by Carlos R. Mafra
parent adf1164fba
commit a8261ff053
3 changed files with 132 additions and 92 deletions

View file

@ -79,10 +79,13 @@ char *MakePathName( const char *dir, const char *file )
else else
fullName = malloc( len1 + len2 + 1 ); fullName = malloc( len1 + len2 + 1 );
if( fullName != NULL)
{
memcpy( fullName, dir, len1 ); memcpy( fullName, dir, len1 );
if( dir[len1-1] != '/' ) if( dir[len1-1] != '/' )
fullName[len1++] = '/'; fullName[len1++] = '/';
memcpy( fullName + len1, file, len2 + 1 ); memcpy( fullName + len1, file, len2 + 1 );
}
return fullName; return fullName;
} }

View file

@ -248,7 +248,9 @@ void ReadConfigFile( bool resetConfigStrings )
if(( usersHome = getenv( "HOME" )) != NULL ) if(( usersHome = getenv( "HOME" )) != NULL )
{ {
char *fileName = MakePathName( usersHome, WMAIL_RC_FILE ); char *fileName;
if(( fileName = MakePathName( usersHome, WMAIL_RC_FILE )) != NULL )
{
FILE *f = fopen( fileName, "rt" ); FILE *f = fopen( fileName, "rt" );
if( f != NULL ) if( f != NULL )
@ -337,6 +339,9 @@ void ReadConfigFile( bool resetConfigStrings )
} else { } else {
TRACE( "unable to open config-file \"%s\"\n", fileName ); TRACE( "unable to open config-file \"%s\"\n", fileName );
} }
} else {
TRACE( "unable to allocate config-file\n" );
}
} else { } else {
TRACE( "no $HOME defined - config-file not read\n" ); TRACE( "no $HOME defined - config-file not read\n" );
} }

View file

@ -230,6 +230,12 @@ int main( int argc, char **argv )
config.checksumFileName = MakePathName( usersHome, WMAIL_CHECKSUM_FILE ); config.checksumFileName = MakePathName( usersHome, WMAIL_CHECKSUM_FILE );
} }
if ( config.checksumFileName == NULL)
{
WARNING( "Cannot allocate checksum file-name.\n");
exit( EXIT_FAILURE );
}
TRACE( "using checksum-file \"%s\"\n", config.checksumFileName ); TRACE( "using checksum-file \"%s\"\n", config.checksumFileName );
// parse cmdline-args and overide defaults and cfg-file settings // parse cmdline-args and overide defaults and cfg-file settings
@ -630,6 +636,12 @@ void CheckMaildir()
char *fullName = FileNameConcat( config.mailBox, dirEnt->d_name ); char *fullName = FileNameConcat( config.mailBox, dirEnt->d_name );
struct stat fileStat; struct stat fileStat;
if ( fullName == NULL )
{
WARNING( "Cannot allocate file/path\n" );
break;
}
if( !stat( fullName, &fileStat ) == 0 ) { if( !stat( fullName, &fileStat ) == 0 ) {
WARNING( "Can't stat file/path \"%s\"\n", fullName ); WARNING( "Can't stat file/path \"%s\"\n", fullName );
free( fullName ); free( fullName );
@ -675,6 +687,12 @@ int TraverseDirectory( const char *name, bool isNewMail )
unsigned long checksum = 0; unsigned long checksum = 0;
name_t *name; name_t *name;
if ( fullName == NULL )
{
WARNING( "Cannot allocate file/path\n" );
break;
}
if( !stat( fullName, &fileStat ) == 0 ) { if( !stat( fullName, &fileStat ) == 0 ) {
WARNING( "Can't stat file/path \"%s\"\n", fullName ); WARNING( "Can't stat file/path \"%s\"\n", fullName );
free( fullName ); free( fullName );
@ -823,7 +841,13 @@ void ParseMBoxFile( struct stat *fileStat )
if( SkipSender( buf+6 )) if( SkipSender( buf+6 ))
goto NEXTMAIL; goto NEXTMAIL;
InsertName( ParseFromField( buf+6 ), checksum, FLAG_INITIAL ); char *name;
if(( name = ParseFromField( buf+6 )) == NULL )
{
WARNING( "Could not parse From field\n" );
break;
}
InsertName( name, checksum, FLAG_INITIAL );
++numMails; ++numMails;
fromFound = 0; fromFound = 0;
@ -868,8 +892,13 @@ void ParseMaildirFile( const char *fileName, unsigned long checksum,
if( SkipSender( buf+6 )) if( SkipSender( buf+6 ))
break; break;
InsertName( ParseFromField( buf+6 ), checksum, char *name;
isNewMail ? FLAG_INITIAL : FLAG_READ ); if(( name = ParseFromField( buf+6 )) == NULL )
{
WARNING( "Could not parse From field\n" );
break;
}
InsertName( name, checksum, isNewMail ? FLAG_INITIAL : FLAG_READ );
//++numMails; //++numMails;
} }
@ -894,11 +923,12 @@ char *ParseFromField( char *buf )
int maxLen = strlen( buf ) + 1; int maxLen = strlen( buf ) + 1;
char *comment; char *comment;
// FIXME: Uhm, those mallocs might fail... if(( fullName = malloc( maxLen )) == NULL )
return NULL;
fullName = malloc( maxLen ); if(( addressName = malloc( maxLen )) == NULL )
addressName = malloc( maxLen ); return NULL;
comment = malloc( maxLen ); if(( comment = malloc( maxLen )) == NULL )
return NULL;
memset( fullName, '\0', maxLen ); memset( fullName, '\0', maxLen );
memset( addressName, '\0', maxLen ); memset( addressName, '\0', maxLen );
@ -1080,7 +1110,9 @@ void InsertName( char *name, unsigned long checksum, flag_t flag )
name_t *item; name_t *item;
TRACE( "insertName: %X, \"%s\"\n", checksum, name ); TRACE( "insertName: %X, \"%s\"\n", checksum, name );
item = (name_t *)malloc( sizeof( name_t )); if (( item = malloc( sizeof( name_t ))) == NULL )
return;
item->name = name; /*strdup( name );*/ item->name = name; /*strdup( name );*/
item->checksum = checksum; item->checksum = checksum;
item->flag = flag; item->flag = flag;