Skip to content

readIndex in RedisCache is not atomic — data race between writeBuffer and copyReadBuffer #86

Description

@bigsamich

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions