Skip to content

Commit ec16d5e

Browse files
authored
Merge pull request #1 from maxDcb/codex/investigate-memoryloadlibrary-failure
Fix random shared memory name generation
2 parents f22d342 + e908db9 commit ec16d5e

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

libs/libMemoryModuleDumy/src/MemoryModule.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@
22

33
#include <fstream>
44

5+
#include <chrono>
56
#include <dlfcn.h>
67
#include <fcntl.h>
8+
#include <random>
79
#include <sys/mman.h>
810
#include <sys/utsname.h>
911
#include <unistd.h>
1012

1113

12-
void generateRandomShmName(char *name, size_t length)
14+
void generateRandomShmName(char *name, size_t length)
1315
{
1416
// Define the character set to choose from
1517
const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1618
size_t charsetSize = sizeof(charset) - 1;
1719

18-
// Seed the random number generator (if not already done)
19-
srand(time(NULL));
20+
using rng_type = std::mt19937;
21+
static rng_type rng{
22+
static_cast<rng_type::result_type>(
23+
std::random_device{}() ^
24+
static_cast<unsigned long>(std::chrono::steady_clock::now().time_since_epoch().count()) ^
25+
static_cast<unsigned long>(getpid()))};
26+
27+
std::uniform_int_distribution<size_t> distribution(0, charsetSize - 1);
2028

2129
// Generate random characters
2230
for (size_t i = 0; i < length; i++) {
23-
int randomIndex = rand() % charsetSize;
24-
name[i] = charset[randomIndex];
31+
name[i] = charset[distribution(rng)];
2532
}
2633

2734
// Null-terminate the string
@@ -136,4 +143,4 @@ HMEMORYMODULE MemoryLoadLibrary(const void *moduleData, size_t size)
136143
}
137144

138145
return handle;
139-
}
146+
}

0 commit comments

Comments
 (0)