An attempt to fix some of the coverity issues

master
Vladimir Stackov 2015-02-10 14:40:11 +03:00
parent 89d6171ea1
commit 38bf13ef83
6 changed files with 35 additions and 34 deletions

View File

@ -190,7 +190,7 @@ Config::OpCodes Config::parseToken( const char * option, const OptionType type )
return Config::oBadOption; return Config::oBadOption;
} }
bool Config::parseOrValidate( const char * option, const OptionType type, bool Config::parseOrValidate( const string & option, const OptionType type,
bool validate ) bool validate )
{ {
string prefix; string prefix;
@ -201,21 +201,21 @@ bool Config::parseOrValidate( const char * option, const OptionType type,
prefix.assign( "storable" ); prefix.assign( "storable" );
dPrintf( "%s %s option \"%s\"...\n", ( validate ? "Validating" : "Parsing" ), dPrintf( "%s %s option \"%s\"...\n", ( validate ? "Validating" : "Parsing" ),
prefix.c_str(), option ); prefix.c_str(), option.c_str() );
bool hasValue = false; bool hasValue = false;
size_t optionLength = strlen( option ); size_t optionLength = option.length() + 1;
char optionName[ optionLength ], optionValue[ optionLength ]; 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 ); dPrintf( "%s option %s: %s\n", prefix.c_str(), optionName, optionValue );
hasValue = true; hasValue = true;
} }
else 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; size_t sizeValue;
char suffix[ 16 ]; char suffix[ 16 ];

View File

@ -76,7 +76,7 @@ public:
void showHelp( const OptionType ); void showHelp( const OptionType );
OpCodes parseToken( const char *, 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( const Config &, ConfigInfo * );
Config( ConfigInfo * ); Config( ConfigInfo * );

10
file.cc
View File

@ -44,7 +44,8 @@ void File::erase( std::string const & filename ) throw( exCantErase )
} }
void File::rename( std::string const & from, void File::rename( std::string const & from,
std::string const & to ) throw( exCantRename ) std::string const & to ) throw( exCantRename,
exCantErase )
{ {
int res = 0; int res = 0;
res = ::rename( from.c_str(), to.c_str() ); res = ::rename( from.c_str(), to.c_str() );
@ -60,13 +61,14 @@ void File::rename( std::string const & from,
/* Open the input file. */ /* Open the input file. */
read_fd = ::open( from.c_str(), O_RDONLY ); read_fd = ::open( from.c_str(), O_RDONLY );
/* Stat the input file to obtain its size. */ /* 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 /* Open the output file for writing, with the same permissions as the
source file. */ source file. */
write_fd = ::open( to.c_str(), O_WRONLY | O_CREAT, stat_buf.st_mode ); write_fd = ::open( to.c_str(), O_WRONLY | O_CREAT, stat_buf.st_mode );
/* Blast the bytes from one file to the other. */ /* Blast the bytes from one file to the other. */
#if defined( __APPLE__ ) #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 ); throw exCantRename( from + " to " + to );
#elif defined( __OpenBSD__ ) #elif defined( __OpenBSD__ )
@ -80,7 +82,7 @@ void File::rename( std::string const & from,
throw exCantRename( from + " to " + to ); throw exCantRename( from + " to " + to );
#else #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 ); throw exCantRename( from + " to " + to );
#endif #endif

View File

@ -138,7 +138,8 @@ public:
/// Renames the given file /// Renames the given file
static void rename( std::string const & from, 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 /// Throwing this class instead of exReadError will make the description
/// include the file name /// include the file name

View File

@ -3,6 +3,7 @@
#include "tmp_mgr.hh" #include "tmp_mgr.hh"
#include <sys/stat.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "dir.hh" #include "dir.hh"
@ -43,6 +44,7 @@ sptr< TemporaryFile > TmpMgr::makeTemporaryFile()
{ {
string name( Dir::addPath( path, "XXXXXX") ); string name( Dir::addPath( path, "XXXXXX") );
umask( S_IRUSR | S_IWUSR | S_IRGRP );
int fd = mkstemp( &name[ 0 ] ); int fd = mkstemp( &name[ 0 ] );
if ( fd == -1 || close( fd ) != 0 ) if ( fd == -1 || close( fd ) != 0 )

View File

@ -24,8 +24,7 @@ int main( int argc, char *argv[] )
for( int x = 1; x < argc; ++x ) for( int x = 1; x < argc; ++x )
{ {
char const * option; string option;
string deprecated;
Config::OptionType optionType = Config::Runtime; Config::OptionType optionType = Config::Runtime;
if ( strcmp( argv[ x ], "--password-file" ) == 0 && x + 1 < argc ) 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 ) if ( strcmp( argv[ x ], "--exchange" ) == 0 && x + 1 < argc )
{ {
fprintf( stderr, "%s is deprecated, use -O exchange instead\n", argv[ x ] ); fprintf( stderr, "%s is deprecated, use -O exchange instead\n", argv[ x ] );
deprecated = argv[ x ] + 2;//; + "=" + argv[ x + 1 ]; option = argv[ x ] + 2;//; + "=" + argv[ x + 1 ];
deprecated += "="; option += "=";
deprecated += argv[ x + 1 ]; option += argv[ x + 1 ];
option = deprecated.c_str();
goto parse_option; goto parse_option;
} }
else else
if ( strcmp( argv[ x ], "--threads" ) == 0 && x + 1 < argc ) if ( strcmp( argv[ x ], "--threads" ) == 0 && x + 1 < argc )
{ {
fprintf( stderr, "%s is deprecated, use -O threads instead\n", argv[ x ] ); fprintf( stderr, "%s is deprecated, use -O threads instead\n", argv[ x ] );
deprecated = argv[ x ] + 2; option = argv[ x ] + 2;
deprecated += "="; option += "=";
deprecated += argv[ x + 1 ]; option += argv[ x + 1 ];
option = deprecated.c_str();
goto parse_option; goto parse_option;
} }
else else
@ -85,20 +82,19 @@ int main( int argc, char *argv[] )
int n; int n;
if ( sscanf( argv[ x + 1 ], "%zu %15s %n", if ( sscanf( argv[ x + 1 ], "%zu %15s %n",
&cacheSizeMb, suffix, &n ) == 2 && !argv[ x + 1][ n ] ) &cacheSizeMb, suffix, &n ) == 2 && !argv[ x + 1][ n ] )
{
deprecated = argv[ x ] + 2; option = argv[ x ] + 2;
deprecated += "=" + Utils::numberToString( cacheSizeMb ) + "MiB"; option += "=" + Utils::numberToString( cacheSizeMb ) + "MiB";
option = deprecated.c_str(); goto parse_option;
goto parse_option; }
} }
else else
if ( strcmp( argv[ x ], "--compression" ) == 0 && x + 1 < argc ) if ( strcmp( argv[ x ], "--compression" ) == 0 && x + 1 < argc )
{ {
fprintf( stderr, "%s is deprecated, use -o bundle.compression_method instead\n", argv[ x ] ); fprintf( stderr, "%s is deprecated, use -o bundle.compression_method instead\n", argv[ x ] );
deprecated = argv[ x ] + 2; option = argv[ x ] + 2;
deprecated += "="; option += "=";
deprecated += argv[ x + 1 ]; option += argv[ x + 1 ];
option = deprecated.c_str();
optionType = Config::Storable; optionType = Config::Storable;
goto parse_option; goto parse_option;
} }
@ -112,7 +108,7 @@ int main( int argc, char *argv[] )
&& x + 1 < argc ) && x + 1 < argc )
{ {
option = argv[ x + 1 ]; option = argv[ x + 1 ];
if ( option ) if ( !option.empty() )
{ {
if ( strcmp( argv[ x ], "-O" ) == 0 ) if ( strcmp( argv[ x ], "-O" ) == 0 )
optionType = Config::Runtime; optionType = Config::Runtime;
@ -120,7 +116,7 @@ int main( int argc, char *argv[] )
if ( strcmp( argv[ x ], "-o" ) == 0 ) if ( strcmp( argv[ x ], "-o" ) == 0 )
optionType = Config::Storable; optionType = Config::Storable;
if ( strcmp( option, "help" ) == 0 ) if ( strcmp( option.c_str(), "help" ) == 0 )
{ {
config.showHelp( optionType ); config.showHelp( optionType );
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -136,7 +132,7 @@ parse_option:
{ {
invalid_option: invalid_option:
fprintf( stderr, "Invalid option specified: %s\n", fprintf( stderr, "Invalid option specified: %s\n",
option ); option.c_str() );
return EXIT_FAILURE; return EXIT_FAILURE;
} }
++x; ++x;