diff options
author | Jack Lloyd <[email protected]> | 2016-10-22 13:49:43 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-10-22 13:49:43 -0400 |
commit | b5f3744536fc7041282740da07ee8fdd0e874aaa (patch) | |
tree | d7818919d71b9157479389e70b175ae45969e436 /src/lib | |
parent | 9033015bb94be08dc76f1bc2138cd85500bad831 (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/lib')
-rw-r--r-- | src/lib/entropy/dev_random/dev_random.cpp | 24 |
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); } } } |