Skip to content

Commit

Permalink
HPCC-27310 Fix Coverity scan issue in lnuid
Browse files Browse the repository at this point in the history
Borrow random_string logic from httplib to populate buffer if
/dev/urandom cannot be opened or read.

Signed-off-by: Tim Klemm <tim.klemm@lexisnexisrisk.com>
  • Loading branch information
Tim Klemm authored and Tim Klemm committed Oct 21, 2024
1 parent ea1047a commit 3f1402e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion system/globalid/lnuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,16 @@ namespace ln_uid {
fp = fopen("/dev/urandom", "r");
if (!fp || fread(&randomdata, 1, random_byte_count, fp) != random_byte_count)
{
// Should never happen, but if it does log it and ignore
// Should never happen, but if it does log it and fallback
OERRLOG("Could not read data from /dev/urandom");
auto randchar = []() -> char {
const char charset[] = "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[static_cast<size_t>(rand()) % max_index];
};
std::generate_n(randomdata, random_byte_count, randchar);
}
if (fp)
fclose(fp);
Expand Down

0 comments on commit 3f1402e

Please sign in to comment.