From: Michael Andres Date: Thu, 4 Oct 2007 14:31:19 +0000 (+0000) Subject: throw constructing malformed checksums. X-Git-Tag: 6.6.0~1550 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2205d57e2c6dcf327476526fffd0ce13193b2f97;p=platform%2Fupstream%2Flibzypp.git throw constructing malformed checksums. --- diff --git a/zypp/CheckSum.cc b/zypp/CheckSum.cc index 983fdbfe5..3a4762a85 100644 --- a/zypp/CheckSum.cc +++ b/zypp/CheckSum.cc @@ -11,6 +11,7 @@ */ #include "zypp/base/Logger.h" +#include "zypp/base/Gettext.h" #include "zypp/base/String.h" #include "zypp/CheckSum.h" @@ -43,15 +44,15 @@ namespace zypp , _checksum( checksum ) { switch ( checksum.size() ) - { + { case 64: if ( _type == sha256Type() ) return; if ( _type.empty() || _type == shaType() ) - { - _type = sha256Type(); - return; - } + { + _type = sha256Type(); + return; + } // else: dubious break; @@ -59,10 +60,10 @@ namespace zypp if ( _type == sha1Type() ) return; if ( _type.empty() || _type == shaType() ) - { - _type = sha1Type(); - return; - } + { + _type = sha1Type(); + return; + } // else: dubious break; @@ -70,10 +71,10 @@ namespace zypp if ( _type == md5Type() ) return; if ( _type.empty() ) - { - _type = md5Type(); - return; - } + { + _type = md5Type(); + return; + } // else: dubious break; @@ -83,16 +84,28 @@ namespace zypp default: if ( _type.empty() ) - { - WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl; - return; - } + { + WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl; + return; + } // else: dubious break; - } - - // dubious - WAR << "Dubious type '" << _type << "' for " << checksum.size() << " byte checksum '" << _checksum << "'" << endl; + } + + // dubious: Throw on malformed known types, otherwise log a warning. + std::string msg = str::form ( _("Dubious type '%s' for %u byte checksum '%s'"), + _type.c_str(), checksum.size(), _checksum.c_str() ); + if ( _type == md5Type() + || _type == shaType() + || _type == sha1Type() + || _type == sha256Type() ) + { + ZYPP_THROW( CheckSumException( msg ) ); + } + else + { + WAR << msg << endl; + } } CheckSum::CheckSum( const std::string & type_r, std::istream & input_r ) @@ -116,7 +129,7 @@ namespace zypp bool CheckSum::empty() const { return (checksum().empty() || type().empty()); } - + std::ostream & operator<<( std::ostream & str, const CheckSum & obj ) { if ( obj.checksum().empty() ) diff --git a/zypp/CheckSum.h b/zypp/CheckSum.h index 7daeeaebb..398a553f6 100644 --- a/zypp/CheckSum.h +++ b/zypp/CheckSum.h @@ -15,19 +15,26 @@ #include #include +#include "zypp/base/Exception.h" #include "zypp/Pathname.h" /////////////////////////////////////////////////////////////////// namespace zypp { ///////////////////////////////////////////////////////////////// + struct CheckSumException : public Exception + { + CheckSumException( const std::string & msg ) + : Exception( msg ) + {} + }; class CheckSum { public: /** * Creates a checksum for algorithm \param type - * \throws if the checksum is invalid and can't be constructed + * \throws CheckSumException if the checksum is invalid and can't be constructed */ CheckSum( const std::string & type, const std::string & checksum); CheckSum( const std::string & type, std::istream & input_r );