Skip to content

Commit 4d3be26

Browse files
MinyazevRiakov
authored andcommitted
Improve code
1 parent b560e40 commit 4d3be26

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

sanitizers/sanitizers.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,44 @@
1515
#include "sanitizers.h"
1616
#include <cstdio>
1717
#include <mutex>
18-
#include <cstring>
18+
#include <string>
1919

2020
#ifdef HAS_SANITIZER_INTERFACE
2121

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;
2325

2426
extern "C" {
2527
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;
3034
}
3135
}
3236

3337
// 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+
}
3541

3642
// 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+
}
3846

3947
// NOLINTNEXTLINE(bugprone-reserved-identifier)
4048
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);
4350

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");
4853
}
4954

50-
auto* const out = file ? file : stderr;
55+
FILE* out = sanitizerFile ? sanitizerFile : stderr;
5156
fputs(str, out);
5257
fflush(out);
5358
}

sanitizers/sanitizers.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License. */
1414

1515
#pragma once
16-
#include <atomic>
16+
#include <string>
1717

1818
#if defined(__SANITIZE_ADDRESS__)
1919
# define HAS_ASAN 1
@@ -35,8 +35,5 @@
3535

3636
#ifdef HAS_SANITIZER_INTERFACE
3737
#include <sanitizer/common_interface_defs.h>
38-
39-
extern std::atomic<char*> globalSanitizerLogPath;
40-
4138
extern "C" void initSanitizerPath(const char* path);
4239
#endif

0 commit comments

Comments
 (0)