canonical / dqlite

Embeddable, replicated and fault-tolerant SQL engine.
https://dqlite.io
Other
3.77k stars 213 forks source link

Multi threading bug in tracef() #540

Closed just-now closed 7 months ago

just-now commented 7 months ago

In multi-threading environment, using static quantifier in line 22 near by static char _msg[1024]; may cause data corruption. It's worth removing static.

#define tracef(...)                                                         \                                                                                   
        do {                                                                \                                                                                   
                if (UNLIKELY(_dqliteTracingEnabled)) {                      \                                                                                   
                        static char _msg[1024];                             \                                                                                   
                        snprintf(_msg, sizeof(_msg), __VA_ARGS__);          \                                                                                   
                        struct timespec ts = {0};                           \                                                                                   
                        /* Ignore errors */                                 \                                                                                   
                        clock_gettime(CLOCK_REALTIME, &ts);                 \                                                                                   
                        int64_t ns = ts.tv_sec * 1000000000 + ts.tv_nsec;   \                                                                                   
                        fprintf(stderr, "LIBDQLITE %" PRId64 " %s:%d %s\n", \                                                                                   
                                ns, __func__, __LINE__, _msg);              \                                                                                   
                }                                                           \                                                                                   
        } while (0)                                                                                                                                             
MathieuBordere commented 7 months ago

fixed in #542