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 );
memcpy( fullName, dir, len1 ); if( fullName != NULL)
if( dir[len1-1] != '/' ) {
fullName[len1++] = '/'; memcpy( fullName, dir, len1 );
memcpy( fullName + len1, file, len2 + 1 ); if( dir[len1-1] != '/' )
fullName[len1++] = '/';
memcpy( fullName + len1, file, len2 + 1 );
}
return fullName; return fullName;
} }

View file

@ -248,94 +248,99 @@ void ReadConfigFile( bool resetConfigStrings )
if(( usersHome = getenv( "HOME" )) != NULL ) if(( usersHome = getenv( "HOME" )) != NULL )
{ {
char *fileName = MakePathName( usersHome, WMAIL_RC_FILE ); char *fileName;
FILE *f = fopen( fileName, "rt" ); if(( fileName = MakePathName( usersHome, WMAIL_RC_FILE )) != NULL )
if( f != NULL )
{ {
char buf[1024]; FILE *f = fopen( fileName, "rt" );
int line = 1;
for( ; !feof( f ); ++line ) if( f != NULL )
{ {
const char *id, *value; char buf[1024];
unsigned int len; int line = 1;
if( fgets( buf, 1024, f ) == NULL ) for( ; !feof( f ); ++line )
break; {
const char *id, *value;
unsigned int len;
// first eliminate the trailing whitespaces if( fgets( buf, 1024, f ) == NULL )
for( len = strlen( buf ); break;
len > 0 && IsWhiteSpace(buf+(--len)); )
*(buf+len) = '\0';
if( !Tokenize( buf, &id, &value )) // first eliminate the trailing whitespaces
continue; for( len = strlen( buf );
len > 0 && IsWhiteSpace(buf+(--len)); )
*(buf+len) = '\0';
if( !strncasecmp( id, "Window.Display", 14 )) { if( !Tokenize( buf, &id, &value ))
if( !( config.givenOptions & CL_DISPLAY )) continue;
ReadString( value, line, &config.display );
} else if( !strncasecmp( id, "Window.NonShaped", 16 )) { if( !strncasecmp( id, "Window.Display", 14 )) {
if( !( config.givenOptions & CL_NOSHAPE )) if( !( config.givenOptions & CL_DISPLAY ))
ReadBool( value, line, &config.noshape ); ReadString( value, line, &config.display );
} else if( !strncasecmp( id, "Window.Button.Command", 21 )) { } else if( !strncasecmp( id, "Window.NonShaped", 16 )) {
if( !( config.givenOptions & CL_RUNCMD )) if( !( config.givenOptions & CL_NOSHAPE ))
ReadString( value, line, &config.runCmd ); ReadBool( value, line, &config.noshape );
} else if( !strncasecmp( id, "Mail.MailBox", 12 )) { } else if( !strncasecmp( id, "Window.Button.Command", 21 )) {
if( !( config.givenOptions & CL_MAILBOX )) if( !( config.givenOptions & CL_RUNCMD ))
ReadString( value, line, &config.mailBox ); ReadString( value, line, &config.runCmd );
} else if( !strncasecmp( id, "Mail.ChecksumFile", 17 )) // no corresponding cmdline option } else if( !strncasecmp( id, "Mail.MailBox", 12 )) {
ReadString( value, line, &config.checksumFileName ); if( !( config.givenOptions & CL_MAILBOX ))
else if( !strncasecmp( id, "Mail.CheckIntervall", 19 )) { ReadString( value, line, &config.mailBox );
if( !( config.givenOptions & CL_CHECKINTERVAL )) } else if( !strncasecmp( id, "Mail.ChecksumFile", 17 )) // no corresponding cmdline option
ReadInt( value, line, &config.checkInterval ); ReadString( value, line, &config.checksumFileName );
} else if( !strncasecmp( id, "Mail.ShowOnlyNew", 16 )) { else if( !strncasecmp( id, "Mail.CheckIntervall", 19 )) {
if( !( config.givenOptions & CL_NEWMAILONLY )) if( !( config.givenOptions & CL_CHECKINTERVAL ))
ReadBool( value, line, &config.newMailsOnly ); ReadInt( value, line, &config.checkInterval );
} else if( !strncasecmp( id, "Ticker.Mode", 11 )) { } else if( !strncasecmp( id, "Mail.ShowOnlyNew", 16 )) {
if( !( config.givenOptions & CL_TICKERMODE )) if( !( config.givenOptions & CL_NEWMAILONLY ))
ReadEnum( value, line, (int *)&config.tickerMode, tickerEnum ); ReadBool( value, line, &config.newMailsOnly );
} else if( !strncasecmp( id, "Ticker.Frames", 13 )) { } else if( !strncasecmp( id, "Ticker.Mode", 11 )) {
if( !( config.givenOptions & CL_FPS )) if( !( config.givenOptions & CL_TICKERMODE ))
ReadInt( value, line, &config.fps ); ReadEnum( value, line, (int *)&config.tickerMode, tickerEnum );
} else if( !strncasecmp( id, "Colors.Symbols", 14 )) { } else if( !strncasecmp( id, "Ticker.Frames", 13 )) {
if( !( config.givenOptions & CL_SYMBOLCOLOR )) if( !( config.givenOptions & CL_FPS ))
ReadString( value, line, &config.symbolColor ); ReadInt( value, line, &config.fps );
} else if( !strncasecmp( id, "Colors.Font", 11 )) { } else if( !strncasecmp( id, "Colors.Symbols", 14 )) {
if( !( config.givenOptions & CL_FONTCOLOR )) if( !( config.givenOptions & CL_SYMBOLCOLOR ))
ReadString( value, line, &config.fontColor ); ReadString( value, line, &config.symbolColor );
} else if( !strncasecmp( id, "Colors.Backlight", 16 )) { } else if( !strncasecmp( id, "Colors.Font", 11 )) {
if( !( config.givenOptions & CL_BACKCOLOR )) if( !( config.givenOptions & CL_FONTCOLOR ))
ReadString( value, line, &config.backColor ); ReadString( value, line, &config.fontColor );
} else if( !strncasecmp( id, "Colors.OffLight", 15 )) { } else if( !strncasecmp( id, "Colors.Backlight", 16 )) {
if( !( config.givenOptions & CL_OFFLIGHTCOLOR )) if( !( config.givenOptions & CL_BACKCOLOR ))
ReadString( value, line, &config.offlightColor ); ReadString( value, line, &config.backColor );
} else if( !strncasecmp( id, "Colors.NonShapedFrame", 21 )) { } else if( !strncasecmp( id, "Colors.OffLight", 15 )) {
if( !( config.givenOptions & CL_NOSHAPE )) if( !( config.givenOptions & CL_OFFLIGHTCOLOR ))
ReadString( value, line, &config.backgroundColor ); ReadString( value, line, &config.offlightColor );
} else if( !strncasecmp( id, "Ticker.X11Font", 14 )) { } else if( !strncasecmp( id, "Colors.NonShapedFrame", 21 )) {
if( !( config.givenOptions & CL_USEX11FONT )) if( !( config.givenOptions & CL_NOSHAPE ))
ReadString( value, line, &config.useX11Font ); ReadString( value, line, &config.backgroundColor );
} else if( !strncasecmp( id, "Mail.SkipSender", 15 )) { // no corresponding cmdline options } else if( !strncasecmp( id, "Ticker.X11Font", 14 )) {
char *skip; if( !( config.givenOptions & CL_USEX11FONT ))
if( ReadString( value, line, &skip )) ReadString( value, line, &config.useX11Font );
AddSenderToSkipList( skip ); } else if( !strncasecmp( id, "Mail.SkipSender", 15 )) { // no corresponding cmdline options
} else if( !strncasecmp( id, "Mail.OnNew.Command", 18 )) { char *skip;
if( !( config.givenOptions & CL_CMDONMAIL )) if( ReadString( value, line, &skip ))
ReadString( value, line, &config.cmdOnMail ); AddSenderToSkipList( skip );
} else if( !strncasecmp( id, "Mail.UseStatusField", 19 )) { } else if( !strncasecmp( id, "Mail.OnNew.Command", 18 )) {
if( !( config.givenOptions & CL_CONSIDERSTATUSFIELD )) if( !( config.givenOptions & CL_CMDONMAIL ))
ReadBool( value, line, &config.considerStatusField ); ReadString( value, line, &config.cmdOnMail );
} else if( !strncasecmp( id, "Mail.ReadStatus", 15 )) { } else if( !strncasecmp( id, "Mail.UseStatusField", 19 )) {
if( !( config.givenOptions & CL_READSTATUS )) if( !( config.givenOptions & CL_CONSIDERSTATUSFIELD ))
ReadString( value, line, &config.readStatus ); ReadBool( value, line, &config.considerStatusField );
} else } else if( !strncasecmp( id, "Mail.ReadStatus", 15 )) {
WARNING( "cfg-file(%i): unrecognized: \"%s\"\n", line, buf ); if( !( config.givenOptions & CL_READSTATUS ))
ReadString( value, line, &config.readStatus );
} else
WARNING( "cfg-file(%i): unrecognized: \"%s\"\n", line, buf );
}
fclose( f );
} else {
TRACE( "unable to open config-file \"%s\"\n", fileName );
} }
fclose( f );
} else { } else {
TRACE( "unable to open config-file \"%s\"\n", fileName ); 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;