|
15 | 15 | #include "sanitizers.h" |
16 | 16 | #include <cstdio> |
17 | 17 | #include <mutex> |
18 | | -#include <cstring> |
| 18 | +#include <string> |
19 | 19 |
|
20 | 20 | #ifdef HAS_SANITIZER_INTERFACE |
21 | 21 |
|
22 | | -std::atomic<char*> globalSanitizerLogPath{nullptr}; |
| 22 | +static std::string globalSanitizerLogPath; // clazy:exclude=non-pod-global-static |
| 23 | +static std::mutex sanitizerMutex; |
| 24 | +static FILE* sanitizerFile = nullptr; |
23 | 25 |
|
24 | 26 | extern "C" { |
25 | 27 | void initSanitizerPath(const char* path) { |
26 | | - if (auto* const newPath = strdup(path)) { |
27 | | - if (auto* const oldPath = globalSanitizerLogPath.exchange(newPath)) { |
28 | | - free(oldPath); |
29 | | - } |
| 28 | + if (!path) return; |
| 29 | + std::lock_guard<std::mutex> lock(sanitizerMutex); |
| 30 | + globalSanitizerLogPath = path; |
| 31 | + if (sanitizerFile) { |
| 32 | + fclose(sanitizerFile); |
| 33 | + sanitizerFile = nullptr; |
30 | 34 | } |
31 | 35 | } |
32 | 36 |
|
33 | 37 | // NOLINTNEXTLINE(bugprone-reserved-identifier) |
34 | | - const char* __asan_default_options() { return "detect_leaks=1"; } |
| 38 | + const char* __asan_default_options() { |
| 39 | + return "detect_leaks=1"; |
| 40 | + } |
35 | 41 |
|
36 | 42 | // NOLINTNEXTLINE(bugprone-reserved-identifier) |
37 | | - const char* __ubsan_default_options() { return "print_stacktrace=1"; } |
| 43 | + const char* __ubsan_default_options() { |
| 44 | + return "print_stacktrace=1"; |
| 45 | + } |
38 | 46 |
|
39 | 47 | // NOLINTNEXTLINE(bugprone-reserved-identifier) |
40 | 48 | void __sanitizer_on_print(const char* str) { |
41 | | - static FILE* file = nullptr; |
42 | | - static std::mutex mutex; |
| 49 | + std::lock_guard<std::mutex> lock(sanitizerMutex); |
43 | 50 |
|
44 | | - std::lock_guard<std::mutex> lock(mutex); |
45 | | - auto* const path = globalSanitizerLogPath.load(); |
46 | | - if (path && !file) { |
47 | | - file = fopen(path, "a"); |
| 51 | + if (!sanitizerFile && !globalSanitizerLogPath.empty()) { |
| 52 | + sanitizerFile = fopen(globalSanitizerLogPath.c_str(), "a"); |
48 | 53 | } |
49 | 54 |
|
50 | | - auto* const out = file ? file : stderr; |
| 55 | + FILE* out = sanitizerFile ? sanitizerFile : stderr; |
51 | 56 | fputs(str, out); |
52 | 57 | fflush(out); |
53 | 58 | } |
|
0 commit comments