Fix false positive in tsan (fixes #4755)

This commit is contained in:
Victor Zverovich 2026-04-29 16:55:06 -07:00
parent eeff8680ed
commit 432fda7bfa

View File

@ -8,6 +8,11 @@
#ifndef FMT_FORMAT_INL_H_ #ifndef FMT_FORMAT_INL_H_
#define FMT_FORMAT_INL_H_ #define FMT_FORMAT_INL_H_
#ifdef __SANITIZE_THREAD__
extern "C" void __tsan_acquire(void*);
extern "C" void __tsan_release(void*);
#endif
#ifndef FMT_MODULE #ifndef FMT_MODULE
# include <stddef.h> // ptrdiff_t # include <stddef.h> // ptrdiff_t
@ -1696,6 +1701,9 @@ class file_print_buffer<F, enable_if_t<has_flockfile<F>::value>>
public: public:
explicit file_print_buffer(F* f) : buffer(grow, size_t()), file_(f) { explicit file_print_buffer(F* f) : buffer(grow, size_t()), file_(f) {
flockfile(f); flockfile(f);
#ifdef __SANITIZE_THREAD__
__tsan_acquire(f);
#endif
file_.init_buffer(); file_.init_buffer();
auto buf = file_.get_write_buffer(); auto buf = file_.get_write_buffer();
set(buf.data, buf.size); set(buf.data, buf.size);
@ -1703,7 +1711,10 @@ class file_print_buffer<F, enable_if_t<has_flockfile<F>::value>>
~file_print_buffer() { ~file_print_buffer() {
file_.advance_write_buffer(size()); file_.advance_write_buffer(size());
bool flush = file_.needs_flush(); bool flush = file_.needs_flush();
F* f = file_; // Make funlockfile depend on the template parameter F F* f = file_; // Make funlockfile depend on the template parameter F.
#ifdef __SANITIZE_THREAD__
__tsan_release(f);
#endif
funlockfile(f); // for the system API detection to work. funlockfile(f); // for the system API detection to work.
if (flush) fflush(file_); if (flush) fflush(file_);
} }