aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-10-22 13:49:43 -0400
committerJack Lloyd <[email protected]>2016-10-22 13:49:43 -0400
commitb5f3744536fc7041282740da07ee8fdd0e874aaa (patch)
treed7818919d71b9157479389e70b175ae45969e436 /src
parent9033015bb94be08dc76f1bc2138cd85500bad831 (diff)
Fix handling of file descriptor zero
If opening /dev/*random resulted in fd 0, we would both not use that RNG and leak the file descriptor. Found with Coverity.
Diffstat (limited to 'src')
-rw-r--r--src/lib/entropy/dev_random/dev_random.cpp24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/lib/entropy/dev_random/dev_random.cpp b/src/lib/entropy/dev_random/dev_random.cpp
index b51f19ecb..f37831d2e 100644
--- a/src/lib/entropy/dev_random/dev_random.cpp
+++ b/src/lib/entropy/dev_random/dev_random.cpp
@@ -38,18 +38,7 @@ Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnam
{
int fd = ::open(fsname.c_str(), flags);
- if(fd > 0)
- {
- if(fd > FD_SETSIZE)
- {
- ::close(fd);
- throw Exception("Open of OS RNG succeeded but fd is too large for fd_set");
- }
-
- m_dev_fds.push_back(fd);
- m_max_fd = std::max(m_max_fd, fd);
- }
- else
+ if(fd < 0)
{
/*
ENOENT or EACCES is normal as some of the named devices may not exist
@@ -57,10 +46,19 @@ Device_EntropySource::Device_EntropySource(const std::vector<std::string>& fsnam
either a bug in the application or file descriptor exhaustion.
*/
if(errno != ENOENT && errno != EACCES)
- {
throw Exception("Opening OS RNG device failed with errno " +
std::to_string(errno));
+ }
+ else
+ {
+ if(fd > FD_SETSIZE)
+ {
+ ::close(fd);
+ throw Exception("Open of OS RNG succeeded but fd is too large for fd_set");
}
+
+ m_dev_fds.push_back(fd);
+ m_max_fd = std::max(m_max_fd, fd);
}
}
}