From a8261ff053c498558968ba961fb1bba8b884ffd4 Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Mon, 27 May 2019 22:52:06 +0100 Subject: [PATCH] wmail: handle allocation failures. --- wmail/src/common.c | 11 +-- wmail/src/config.c | 163 +++++++++++++++++++++++---------------------- wmail/src/wmail.c | 50 +++++++++++--- 3 files changed, 132 insertions(+), 92 deletions(-) diff --git a/wmail/src/common.c b/wmail/src/common.c index 59acec4..64100d1 100644 --- a/wmail/src/common.c +++ b/wmail/src/common.c @@ -79,10 +79,13 @@ char *MakePathName( const char *dir, const char *file ) else fullName = malloc( len1 + len2 + 1 ); - memcpy( fullName, dir, len1 ); - if( dir[len1-1] != '/' ) - fullName[len1++] = '/'; - memcpy( fullName + len1, file, len2 + 1 ); + if( fullName != NULL) + { + memcpy( fullName, dir, len1 ); + if( dir[len1-1] != '/' ) + fullName[len1++] = '/'; + memcpy( fullName + len1, file, len2 + 1 ); + } return fullName; } diff --git a/wmail/src/config.c b/wmail/src/config.c index 02b9ae1..546afa5 100644 --- a/wmail/src/config.c +++ b/wmail/src/config.c @@ -248,94 +248,99 @@ void ReadConfigFile( bool resetConfigStrings ) if(( usersHome = getenv( "HOME" )) != NULL ) { - char *fileName = MakePathName( usersHome, WMAIL_RC_FILE ); - FILE *f = fopen( fileName, "rt" ); - - if( f != NULL ) + char *fileName; + if(( fileName = MakePathName( usersHome, WMAIL_RC_FILE )) != NULL ) { - char buf[1024]; - int line = 1; + FILE *f = fopen( fileName, "rt" ); - for( ; !feof( f ); ++line ) + if( f != NULL ) { - const char *id, *value; - unsigned int len; + char buf[1024]; + int line = 1; - if( fgets( buf, 1024, f ) == NULL ) - break; + for( ; !feof( f ); ++line ) + { + const char *id, *value; + unsigned int len; - // first eliminate the trailing whitespaces - for( len = strlen( buf ); - len > 0 && IsWhiteSpace(buf+(--len)); ) - *(buf+len) = '\0'; + if( fgets( buf, 1024, f ) == NULL ) + break; - if( !Tokenize( buf, &id, &value )) - continue; + // first eliminate the trailing whitespaces + for( len = strlen( buf ); + len > 0 && IsWhiteSpace(buf+(--len)); ) + *(buf+len) = '\0'; - if( !strncasecmp( id, "Window.Display", 14 )) { - if( !( config.givenOptions & CL_DISPLAY )) - ReadString( value, line, &config.display ); - } else if( !strncasecmp( id, "Window.NonShaped", 16 )) { - if( !( config.givenOptions & CL_NOSHAPE )) - ReadBool( value, line, &config.noshape ); - } else if( !strncasecmp( id, "Window.Button.Command", 21 )) { - if( !( config.givenOptions & CL_RUNCMD )) - ReadString( value, line, &config.runCmd ); - } else if( !strncasecmp( id, "Mail.MailBox", 12 )) { - if( !( config.givenOptions & CL_MAILBOX )) - ReadString( value, line, &config.mailBox ); - } else if( !strncasecmp( id, "Mail.ChecksumFile", 17 )) // no corresponding cmdline option - ReadString( value, line, &config.checksumFileName ); - else if( !strncasecmp( id, "Mail.CheckIntervall", 19 )) { - if( !( config.givenOptions & CL_CHECKINTERVAL )) - ReadInt( value, line, &config.checkInterval ); - } else if( !strncasecmp( id, "Mail.ShowOnlyNew", 16 )) { - if( !( config.givenOptions & CL_NEWMAILONLY )) - ReadBool( value, line, &config.newMailsOnly ); - } else if( !strncasecmp( id, "Ticker.Mode", 11 )) { - if( !( config.givenOptions & CL_TICKERMODE )) - ReadEnum( value, line, (int *)&config.tickerMode, tickerEnum ); - } else if( !strncasecmp( id, "Ticker.Frames", 13 )) { - if( !( config.givenOptions & CL_FPS )) - ReadInt( value, line, &config.fps ); - } else if( !strncasecmp( id, "Colors.Symbols", 14 )) { - if( !( config.givenOptions & CL_SYMBOLCOLOR )) - ReadString( value, line, &config.symbolColor ); - } else if( !strncasecmp( id, "Colors.Font", 11 )) { - if( !( config.givenOptions & CL_FONTCOLOR )) - ReadString( value, line, &config.fontColor ); - } else if( !strncasecmp( id, "Colors.Backlight", 16 )) { - if( !( config.givenOptions & CL_BACKCOLOR )) - ReadString( value, line, &config.backColor ); - } else if( !strncasecmp( id, "Colors.OffLight", 15 )) { - if( !( config.givenOptions & CL_OFFLIGHTCOLOR )) - ReadString( value, line, &config.offlightColor ); - } else if( !strncasecmp( id, "Colors.NonShapedFrame", 21 )) { - if( !( config.givenOptions & CL_NOSHAPE )) - ReadString( value, line, &config.backgroundColor ); - } else if( !strncasecmp( id, "Ticker.X11Font", 14 )) { - if( !( config.givenOptions & CL_USEX11FONT )) - ReadString( value, line, &config.useX11Font ); - } else if( !strncasecmp( id, "Mail.SkipSender", 15 )) { // no corresponding cmdline options - char *skip; - if( ReadString( value, line, &skip )) - AddSenderToSkipList( skip ); - } else if( !strncasecmp( id, "Mail.OnNew.Command", 18 )) { - if( !( config.givenOptions & CL_CMDONMAIL )) - ReadString( value, line, &config.cmdOnMail ); - } else if( !strncasecmp( id, "Mail.UseStatusField", 19 )) { - if( !( config.givenOptions & CL_CONSIDERSTATUSFIELD )) - ReadBool( value, line, &config.considerStatusField ); - } else if( !strncasecmp( id, "Mail.ReadStatus", 15 )) { - if( !( config.givenOptions & CL_READSTATUS )) - ReadString( value, line, &config.readStatus ); - } else - WARNING( "cfg-file(%i): unrecognized: \"%s\"\n", line, buf ); + if( !Tokenize( buf, &id, &value )) + continue; + + if( !strncasecmp( id, "Window.Display", 14 )) { + if( !( config.givenOptions & CL_DISPLAY )) + ReadString( value, line, &config.display ); + } else if( !strncasecmp( id, "Window.NonShaped", 16 )) { + if( !( config.givenOptions & CL_NOSHAPE )) + ReadBool( value, line, &config.noshape ); + } else if( !strncasecmp( id, "Window.Button.Command", 21 )) { + if( !( config.givenOptions & CL_RUNCMD )) + ReadString( value, line, &config.runCmd ); + } else if( !strncasecmp( id, "Mail.MailBox", 12 )) { + if( !( config.givenOptions & CL_MAILBOX )) + ReadString( value, line, &config.mailBox ); + } else if( !strncasecmp( id, "Mail.ChecksumFile", 17 )) // no corresponding cmdline option + ReadString( value, line, &config.checksumFileName ); + else if( !strncasecmp( id, "Mail.CheckIntervall", 19 )) { + if( !( config.givenOptions & CL_CHECKINTERVAL )) + ReadInt( value, line, &config.checkInterval ); + } else if( !strncasecmp( id, "Mail.ShowOnlyNew", 16 )) { + if( !( config.givenOptions & CL_NEWMAILONLY )) + ReadBool( value, line, &config.newMailsOnly ); + } else if( !strncasecmp( id, "Ticker.Mode", 11 )) { + if( !( config.givenOptions & CL_TICKERMODE )) + ReadEnum( value, line, (int *)&config.tickerMode, tickerEnum ); + } else if( !strncasecmp( id, "Ticker.Frames", 13 )) { + if( !( config.givenOptions & CL_FPS )) + ReadInt( value, line, &config.fps ); + } else if( !strncasecmp( id, "Colors.Symbols", 14 )) { + if( !( config.givenOptions & CL_SYMBOLCOLOR )) + ReadString( value, line, &config.symbolColor ); + } else if( !strncasecmp( id, "Colors.Font", 11 )) { + if( !( config.givenOptions & CL_FONTCOLOR )) + ReadString( value, line, &config.fontColor ); + } else if( !strncasecmp( id, "Colors.Backlight", 16 )) { + if( !( config.givenOptions & CL_BACKCOLOR )) + ReadString( value, line, &config.backColor ); + } else if( !strncasecmp( id, "Colors.OffLight", 15 )) { + if( !( config.givenOptions & CL_OFFLIGHTCOLOR )) + ReadString( value, line, &config.offlightColor ); + } else if( !strncasecmp( id, "Colors.NonShapedFrame", 21 )) { + if( !( config.givenOptions & CL_NOSHAPE )) + ReadString( value, line, &config.backgroundColor ); + } else if( !strncasecmp( id, "Ticker.X11Font", 14 )) { + if( !( config.givenOptions & CL_USEX11FONT )) + ReadString( value, line, &config.useX11Font ); + } else if( !strncasecmp( id, "Mail.SkipSender", 15 )) { // no corresponding cmdline options + char *skip; + if( ReadString( value, line, &skip )) + AddSenderToSkipList( skip ); + } else if( !strncasecmp( id, "Mail.OnNew.Command", 18 )) { + if( !( config.givenOptions & CL_CMDONMAIL )) + ReadString( value, line, &config.cmdOnMail ); + } else if( !strncasecmp( id, "Mail.UseStatusField", 19 )) { + if( !( config.givenOptions & CL_CONSIDERSTATUSFIELD )) + ReadBool( value, line, &config.considerStatusField ); + } else if( !strncasecmp( id, "Mail.ReadStatus", 15 )) { + 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 { - TRACE( "unable to open config-file \"%s\"\n", fileName ); + TRACE( "unable to allocate config-file\n" ); } } else { TRACE( "no $HOME defined - config-file not read\n" ); diff --git a/wmail/src/wmail.c b/wmail/src/wmail.c index 5bbebb9..40d932c 100644 --- a/wmail/src/wmail.c +++ b/wmail/src/wmail.c @@ -230,6 +230,12 @@ int main( int argc, char **argv ) 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 ); // parse cmdline-args and overide defaults and cfg-file settings @@ -630,6 +636,12 @@ void CheckMaildir() char *fullName = FileNameConcat( config.mailBox, dirEnt->d_name ); struct stat fileStat; + if ( fullName == NULL ) + { + WARNING( "Cannot allocate file/path\n" ); + break; + } + if( !stat( fullName, &fileStat ) == 0 ) { WARNING( "Can't stat file/path \"%s\"\n", fullName ); free( fullName ); @@ -675,6 +687,12 @@ int TraverseDirectory( const char *name, bool isNewMail ) unsigned long checksum = 0; name_t *name; + if ( fullName == NULL ) + { + WARNING( "Cannot allocate file/path\n" ); + break; + } + if( !stat( fullName, &fileStat ) == 0 ) { WARNING( "Can't stat file/path \"%s\"\n", fullName ); free( fullName ); @@ -823,7 +841,13 @@ void ParseMBoxFile( struct stat *fileStat ) if( SkipSender( buf+6 )) 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; fromFound = 0; @@ -868,8 +892,13 @@ void ParseMaildirFile( const char *fileName, unsigned long checksum, if( SkipSender( buf+6 )) break; - InsertName( ParseFromField( buf+6 ), checksum, - isNewMail ? FLAG_INITIAL : FLAG_READ ); + char *name; + if(( name = ParseFromField( buf+6 )) == NULL ) + { + WARNING( "Could not parse From field\n" ); + break; + } + InsertName( name, checksum, isNewMail ? FLAG_INITIAL : FLAG_READ ); //++numMails; } @@ -894,11 +923,12 @@ char *ParseFromField( char *buf ) int maxLen = strlen( buf ) + 1; char *comment; - // FIXME: Uhm, those mallocs might fail... - - fullName = malloc( maxLen ); - addressName = malloc( maxLen ); - comment = malloc( maxLen ); + if(( fullName = malloc( maxLen )) == NULL ) + return NULL; + if(( addressName = malloc( maxLen )) == NULL ) + return NULL; + if(( comment = malloc( maxLen )) == NULL ) + return NULL; memset( fullName, '\0', maxLen ); memset( addressName, '\0', maxLen ); @@ -1080,7 +1110,9 @@ void InsertName( char *name, unsigned long checksum, flag_t flag ) name_t *item; 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->checksum = checksum; item->flag = flag;