From 38bf13ef8363e4a5fb5cb7939ada6f4979fa58e2 Mon Sep 17 00:00:00 2001 From: Vladimir Stackov Date: Tue, 10 Feb 2015 14:40:11 +0300 Subject: [PATCH] An attempt to fix some of the coverity issues --- config.cc | 12 ++++++------ config.hh | 2 +- file.cc | 10 ++++++---- file.hh | 3 ++- tmp_mgr.cc | 2 ++ zbackup.cc | 40 ++++++++++++++++++---------------------- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/config.cc b/config.cc index 61edb2a..958d8d8 100644 --- a/config.cc +++ b/config.cc @@ -190,7 +190,7 @@ Config::OpCodes Config::parseToken( const char * option, const OptionType type ) return Config::oBadOption; } -bool Config::parseOrValidate( const char * option, const OptionType type, +bool Config::parseOrValidate( const string & option, const OptionType type, bool validate ) { string prefix; @@ -201,21 +201,21 @@ bool Config::parseOrValidate( const char * option, const OptionType type, prefix.assign( "storable" ); dPrintf( "%s %s option \"%s\"...\n", ( validate ? "Validating" : "Parsing" ), - prefix.c_str(), option ); + prefix.c_str(), option.c_str() ); bool hasValue = false; - size_t optionLength = strlen( option ); + size_t optionLength = option.length() + 1; char optionName[ optionLength ], optionValue[ optionLength ]; - if ( sscanf( option, "%[^=]=%s", optionName, optionValue ) == 2 ) + if ( sscanf( option.c_str(), "%[^=]=%s", optionName, optionValue ) == 2 ) { dPrintf( "%s option %s: %s\n", prefix.c_str(), optionName, optionValue ); hasValue = true; } else - dPrintf( "%s option %s\n", prefix.c_str(), option ); + dPrintf( "%s option %s\n", prefix.c_str(), option.c_str() ); - int opcode = parseToken( hasValue ? optionName : option, type ); + int opcode = parseToken( hasValue ? optionName : option.c_str(), type ); size_t sizeValue; char suffix[ 16 ]; diff --git a/config.hh b/config.hh index 8304dc6..1408e6e 100644 --- a/config.hh +++ b/config.hh @@ -76,7 +76,7 @@ public: void showHelp( const OptionType ); OpCodes parseToken( const char *, const OptionType ); - bool parseOrValidate( const char *, const OptionType, bool validate = false ); + bool parseOrValidate( const string &, const OptionType, bool validate = false ); Config( const Config &, ConfigInfo * ); Config( ConfigInfo * ); diff --git a/file.cc b/file.cc index 972215f..23fe20e 100644 --- a/file.cc +++ b/file.cc @@ -44,7 +44,8 @@ void File::erase( std::string const & filename ) throw( exCantErase ) } void File::rename( std::string const & from, - std::string const & to ) throw( exCantRename ) + std::string const & to ) throw( exCantRename, + exCantErase ) { int res = 0; res = ::rename( from.c_str(), to.c_str() ); @@ -60,13 +61,14 @@ void File::rename( std::string const & from, /* Open the input file. */ read_fd = ::open( from.c_str(), O_RDONLY ); /* Stat the input file to obtain its size. */ - fstat( read_fd, &stat_buf ); + if ( fstat( read_fd, &stat_buf ) != 0 ) + throw exCantRename( from + " to " + to ); /* Open the output file for writing, with the same permissions as the source file. */ write_fd = ::open( to.c_str(), O_WRONLY | O_CREAT, stat_buf.st_mode ); /* Blast the bytes from one file to the other. */ #if defined( __APPLE__ ) - if ( -1 == sendfile(write_fd, read_fd, offset, &stat_buf.st_size, NULL, 0) ) + if ( -1 == sendfile( write_fd, read_fd, offset, &stat_buf.st_size, NULL, 0 ) ) throw exCantRename( from + " to " + to ); #elif defined( __OpenBSD__ ) @@ -80,7 +82,7 @@ void File::rename( std::string const & from, throw exCantRename( from + " to " + to ); #else - if ( -1 == sendfile(write_fd, read_fd, &offset, stat_buf.st_size) ) + if ( -1 == sendfile( write_fd, read_fd, &offset, stat_buf.st_size ) ) throw exCantRename( from + " to " + to ); #endif diff --git a/file.hh b/file.hh index 4e6eba8..b1519d4 100644 --- a/file.hh +++ b/file.hh @@ -138,7 +138,8 @@ public: /// Renames the given file static void rename( std::string const & from, - std::string const & to ) throw( exCantRename ); + std::string const & to ) throw( exCantRename, + exCantErase ); /// Throwing this class instead of exReadError will make the description /// include the file name diff --git a/tmp_mgr.cc b/tmp_mgr.cc index 4c41a25..a022f99 100644 --- a/tmp_mgr.cc +++ b/tmp_mgr.cc @@ -3,6 +3,7 @@ #include "tmp_mgr.hh" +#include #include #include #include "dir.hh" @@ -43,6 +44,7 @@ sptr< TemporaryFile > TmpMgr::makeTemporaryFile() { string name( Dir::addPath( path, "XXXXXX") ); + umask( S_IRUSR | S_IWUSR | S_IRGRP ); int fd = mkstemp( &name[ 0 ] ); if ( fd == -1 || close( fd ) != 0 ) diff --git a/zbackup.cc b/zbackup.cc index e0b1d1a..04479a2 100644 --- a/zbackup.cc +++ b/zbackup.cc @@ -24,8 +24,7 @@ int main( int argc, char *argv[] ) for( int x = 1; x < argc; ++x ) { - char const * option; - string deprecated; + string option; Config::OptionType optionType = Config::Runtime; if ( strcmp( argv[ x ], "--password-file" ) == 0 && x + 1 < argc ) @@ -60,20 +59,18 @@ int main( int argc, char *argv[] ) if ( strcmp( argv[ x ], "--exchange" ) == 0 && x + 1 < argc ) { fprintf( stderr, "%s is deprecated, use -O exchange instead\n", argv[ x ] ); - deprecated = argv[ x ] + 2;//; + "=" + argv[ x + 1 ]; - deprecated += "="; - deprecated += argv[ x + 1 ]; - option = deprecated.c_str(); + option = argv[ x ] + 2;//; + "=" + argv[ x + 1 ]; + option += "="; + option += argv[ x + 1 ]; goto parse_option; } else if ( strcmp( argv[ x ], "--threads" ) == 0 && x + 1 < argc ) { fprintf( stderr, "%s is deprecated, use -O threads instead\n", argv[ x ] ); - deprecated = argv[ x ] + 2; - deprecated += "="; - deprecated += argv[ x + 1 ]; - option = deprecated.c_str(); + option = argv[ x ] + 2; + option += "="; + option += argv[ x + 1 ]; goto parse_option; } else @@ -85,20 +82,19 @@ int main( int argc, char *argv[] ) int n; if ( sscanf( argv[ x + 1 ], "%zu %15s %n", &cacheSizeMb, suffix, &n ) == 2 && !argv[ x + 1][ n ] ) - - deprecated = argv[ x ] + 2; - deprecated += "=" + Utils::numberToString( cacheSizeMb ) + "MiB"; - option = deprecated.c_str(); - goto parse_option; + { + option = argv[ x ] + 2; + option += "=" + Utils::numberToString( cacheSizeMb ) + "MiB"; + goto parse_option; + } } else if ( strcmp( argv[ x ], "--compression" ) == 0 && x + 1 < argc ) { fprintf( stderr, "%s is deprecated, use -o bundle.compression_method instead\n", argv[ x ] ); - deprecated = argv[ x ] + 2; - deprecated += "="; - deprecated += argv[ x + 1 ]; - option = deprecated.c_str(); + option = argv[ x ] + 2; + option += "="; + option += argv[ x + 1 ]; optionType = Config::Storable; goto parse_option; } @@ -112,7 +108,7 @@ int main( int argc, char *argv[] ) && x + 1 < argc ) { option = argv[ x + 1 ]; - if ( option ) + if ( !option.empty() ) { if ( strcmp( argv[ x ], "-O" ) == 0 ) optionType = Config::Runtime; @@ -120,7 +116,7 @@ int main( int argc, char *argv[] ) if ( strcmp( argv[ x ], "-o" ) == 0 ) optionType = Config::Storable; - if ( strcmp( option, "help" ) == 0 ) + if ( strcmp( option.c_str(), "help" ) == 0 ) { config.showHelp( optionType ); return EXIT_SUCCESS; @@ -136,7 +132,7 @@ parse_option: { invalid_option: fprintf( stderr, "Invalid option specified: %s\n", - option ); + option.c_str() ); return EXIT_FAILURE; } ++x;