Base64::Base64: Fix exception safety

Found by cppcheck.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2013-02-17 21:44:36 -05:00
committed by Keith Winstein
parent b6f17917f6
commit 14ef590220
+4 -9
View File
@@ -36,6 +36,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <fstream>
#include "byteorder.h" #include "byteorder.h"
#include "crypto.h" #include "crypto.h"
@@ -121,18 +122,12 @@ Base64Key::Base64Key( string printable_key )
Base64Key::Base64Key() Base64Key::Base64Key()
{ {
FILE *devrandom = fopen( rdev, "r" ); ifstream devrandom( rdev, ifstream::in | ifstream::binary );
if ( devrandom == NULL ) {
throw CryptoException( string( rdev ) + ": " + strerror( errno ) );
}
if ( 1 != fread( key, 16, 1, devrandom ) ) { devrandom.read( reinterpret_cast<char *>( key ), sizeof( key ) );
if ( !devrandom ) {
throw CryptoException( "Could not read from " + string( rdev ) ); throw CryptoException( "Could not read from " + string( rdev ) );
} }
if ( 0 != fclose( devrandom ) ) {
throw CryptoException( string( rdev ) + ": " + strerror( errno ) );
}
} }
string Base64Key::printable_key( void ) const string Base64Key::printable_key( void ) const