Description
In RedisCache.hpp:26, readIndex is a plain int. It is written under the exclusive lock in writeBuffer (line 41), but read outside any lock on line 34:
void writeBuffer(...)
{
int writeIndex = (readIndex + 1) % 2; // line 34 — READ outside lock
buffers.at(writeIndex) = data;
{
std::lock_guard<std::shared_mutex> swapLock(swapMutex);
readIndex = (readIndex + 1) % 2; // line 41 — WRITE under lock
}
}
If writeBuffer is called from the reader callback thread while another thread holds the shared lock for reading, the read on line 34 races with the write on line 41.
Severity
Medium
Suggested Fix
Make readIndex an std::atomic<int>, or access it under the mutex consistently.
Description
In
RedisCache.hpp:26,readIndexis a plainint. It is written under the exclusive lock inwriteBuffer(line 41), but read outside any lock on line 34:If
writeBufferis called from the reader callback thread while another thread holds the shared lock for reading, the read on line 34 races with the write on line 41.Severity
Medium
Suggested Fix
Make
readIndexanstd::atomic<int>, or access it under the mutex consistently.