Skip to content

Commit b85a645

Browse files
Wenfeng LiuFelipe Zimmerle
authored andcommitted
Fix race condition in UniqueId::uniqueId()
1 parent b586fa9 commit b85a645

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/unique_id.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
namespace modsecurity {
5757

58+
std::once_flag UniqueId::onceFlag;
59+
5860
void UniqueId::fillUniqueId() {
5961
std::string macAddress;
6062
std::string name;

src/unique_id.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#ifdef __cplusplus
1717
#include <string>
18+
#include <mutex>
1819
#endif
1920

2021
#ifndef SRC_UNIQUE_ID_H_
@@ -37,9 +38,9 @@ class UniqueId {
3738
}
3839

3940
static std::string uniqueId() {
40-
if (UniqueId::getInstance().uniqueId_str.empty()) {
41+
std::call_once(UniqueId::onceFlag,[]() {
4142
UniqueId::getInstance().fillUniqueId();
42-
}
43+
});
4344

4445
return UniqueId::getInstance().uniqueId_str;
4546
}
@@ -59,6 +60,8 @@ class UniqueId {
5960
// C++ 11
6061
// UniqueId(UniqueId const&) = delete;
6162
// void operator=(UniqueId const&) = delete;
63+
64+
static std::once_flag onceFlag;
6265
};
6366

6467
} // namespace modsecurity

0 commit comments

Comments
 (0)