Skip to content

Commit

Permalink
Use system_error in random_device. Fixes #8245.
Browse files Browse the repository at this point in the history
  • Loading branch information
swatanabe committed Mar 13, 2014
1 parent c0a694e commit dac48f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build/Jamfile.v2
Expand Up @@ -13,6 +13,6 @@ project /boost/random
: usage-requirements <link>shared:<define>BOOST_RANDOM_DYN_LINK
;

lib boost_random : [ glob *.cpp ] ;
lib boost_random : [ glob *.cpp ] /boost//system ;

boost-install boost_random ;
1 change: 1 addition & 0 deletions include/boost/random/random_device.hpp
Expand Up @@ -22,6 +22,7 @@
#include <boost/config.hpp>
#include <boost/noncopyable.hpp>
#include <boost/random/detail/auto_link.hpp>
#include <boost/system/config.hpp> // force autolink to find Boost.System

namespace boost {
namespace random {
Expand Down
41 changes: 13 additions & 28 deletions src/random_device.cpp
Expand Up @@ -17,6 +17,8 @@
#include <boost/throw_exception.hpp>
#include <boost/assert.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/system/system_error.hpp>
#include <boost/system/error_code.hpp>
#include <string>

#if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) && !BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
Expand Down Expand Up @@ -69,7 +71,6 @@ class boost::random::random_device::impl
char buffer[80];
DWORD type;
DWORD len;
bool found (false);

// Find the type of a specific provider
for(DWORD i = 0; ; ++i) {
Expand All @@ -79,13 +80,9 @@ class boost::random::random_device::impl
continue;
}
if(buffer == provider) {
found = true;
break;
}
}
if (!found) {
error("Could not find provider name");
}

if(!CryptAcquireContextA(&hProv, NULL, provider.c_str(), type,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
Expand All @@ -109,27 +106,13 @@ class boost::random::random_device::impl
}

private:
void error(const std::string & msg) {
char buf[80];
void error(const char * msg) {
DWORD error_code = GetLastError();
DWORD num = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_code,
0,
buf,
sizeof(buf),
NULL);

char hex_error[9];
hex_error[8] = 0;
_snprintf(hex_error, sizeof(hex_error), "%08x", error_code);

boost::throw_exception(
std::invalid_argument(
"boost::random_device: " + msg + ": " + hex_error +
" " + std::string(&buf[0], &buf[0] + num) +
" (Cryptographic Service Provider: " + provider + ")"));
boost::system::system_error(
error_code, boost::system::system_category(),
std::string("boost::random_device: ") + msg +
" Cryptographic Service Provider " + provider));
}
const std::string provider;
HCRYPTPROV hProv;
Expand Down Expand Up @@ -196,10 +179,12 @@ class boost::random::random_device::impl
}

private:
void error(const std::string & msg) {
boost::throw_exception(std::invalid_argument("boost::random_device: " + msg +
" random-number pseudo-device " + path +
": " + strerror(errno)));
void error(const char * msg) {
boost::throw_exception(
boost::system::system_error(
errno, boost::system::system_category(),
std::string("boost::random_device: ") + msg +
" random-number pseudo-device " + path));
}
const std::string path;
int fd;
Expand Down

0 comments on commit dac48f6

Please sign in to comment.