Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,12 @@ namespace {

std::string random_alnum_string(int len) {
static constexpr char kDict[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static std::random_device rd;
std::uniform_int_distribution<size_t> dist(0, sizeof(kDict)-1);
static std::mt19937 gen;
static std::uniform_int_distribution<size_t> dist(0, sizeof(kDict)-2);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offline discussion: Why -2? Turns out it is for the null character.

Instead we should consider using:

static constexpr std::string_view kDict = "0123456789abcdefghijklmnopqrstuvwxyz"

And then we can do kDict.size() - 1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d00b129.

std::string res;
res.reserve(len);
for (int i = 0; i < len; ++i) {
res.push_back(kDict[dist(rd)]);
res.push_back(kDict[dist(gen)]);
}
return res;
}
Expand Down