Skip to content

Commit 6e1da0b

Browse files
committed
fix: "Check buffer boundaries if used in a loop including recursive loops (CWE-120, CWE-20)."
1 parent 11b7f58 commit 6e1da0b

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

include/CXXGraph/Utility/SecureRandom.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <stdexcept>
55

66
#if defined(_WIN32) || defined(_WIN64)
7-
#include <windows.h>
87
#include <bcrypt.h>
8+
#include <windows.h>
99
#ifndef STATUS_SUCCESS
1010
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
1111
#endif
@@ -33,10 +33,15 @@ inline void generateBytes(unsigned char* buffer, size_t length) {
3333
if (!urandom.is_open())
3434
throw std::runtime_error("Failed to open /dev/urandom");
3535

36-
urandom.read(reinterpret_cast<char*>(buffer),
37-
static_cast<std::streamsize>(length));
38-
if (urandom.gcount() != static_cast<std::streamsize>(length))
39-
throw std::runtime_error("Failed to read enough random bytes");
36+
std::streamsize totalRead = 0;
37+
while (totalRead < static_cast<std::streamsize>(length)) {
38+
urandom.read(reinterpret_cast<char*>(buffer) + totalRead,
39+
static_cast<std::streamsize>(length) - totalRead);
40+
std::streamsize bytesRead = urandom.gcount();
41+
if (bytesRead <= 0)
42+
throw std::runtime_error("Failed to read enough random bytes");
43+
totalRead += bytesRead;
44+
}
4045
#endif
4146
}
4247

0 commit comments

Comments
 (0)