From 14ef59022019287633809b39c3a8a943d912fba7 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 17 Feb 2013 21:44:36 -0500 Subject: [PATCH] Base64::Base64: Fix exception safety Found by cppcheck. Signed-off-by: Anders Kaseorg --- src/crypto/crypto.cc | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/crypto/crypto.cc b/src/crypto/crypto.cc index 50b60d9..9458de9 100644 --- a/src/crypto/crypto.cc +++ b/src/crypto/crypto.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include "byteorder.h" #include "crypto.h" @@ -121,18 +122,12 @@ Base64Key::Base64Key( string printable_key ) Base64Key::Base64Key() { - FILE *devrandom = fopen( rdev, "r" ); - if ( devrandom == NULL ) { - throw CryptoException( string( rdev ) + ": " + strerror( errno ) ); - } + ifstream devrandom( rdev, ifstream::in | ifstream::binary ); - if ( 1 != fread( key, 16, 1, devrandom ) ) { + devrandom.read( reinterpret_cast( key ), sizeof( key ) ); + if ( !devrandom ) { throw CryptoException( "Could not read from " + string( rdev ) ); } - - if ( 0 != fclose( devrandom ) ) { - throw CryptoException( string( rdev ) + ": " + strerror( errno ) ); - } } string Base64Key::printable_key( void ) const