Skip to content

Over-allocation in MockRedisAdapter list functions using size_bytes() instead of size() #81

Open

Description

@bigsamich

Description

In mock/MockRedisAdapter.hpp:62-64 and 93-95, the list mock functions use data.size_bytes() for the allocation count but allocate elements of type T:

int dataSize = data.size_bytes();   // size in BYTES
new T[dataSize],                    // allocates dataSize ELEMENTS (each sizeof(T) bytes)

This allocates sizeof(T) * size_bytes() total bytes instead of the intended amount.

Additionally, std::vector<T> does not have a .size_bytes() method (line 93), so the vector overload likely won't compile for most types.

Severity

Critical

Suggested Fix

Use data.size() instead of data.size_bytes() for the allocation count.

Activity

  1. bigsamich commented on Sep 30, 2026

    @bigsamich
    ContributorAuthor

    Still present on main, and unchanged through #108/#109/#111 (the mock file is byte-identical). It is worse than described here:

    • Under the project's C++17, no instantiation of either addSingleList overload compiles. std::vector, std::array and the spanCpp14.hpp span have no size_bytes().
    • Only a C++20 std::span compiles, and that one allocates sizeof(T) times too much: 48 bytes for 12.

    CI never compiles the mock, which is how this went unnoticed; filed as #124.

    Validated fix: const size_t count = data.size(); int dataSize = count * sizeof(T); new T[count]. With it, all six vector/array/span × C++17/C++20 combinations compile and run clean under ASan+UBSan, with exactly 12 bytes allocated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmust fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions