Skip to content

Avoid UB in LRUHandle key storage#1327

Open
qingketsing wants to merge 1 commit into
google:mainfrom
qingketsing:fix-lruhandle-key-storage
Open

Avoid UB in LRUHandle key storage#1327
qingketsing wants to merge 1 commit into
google:mainfrom
qingketsing:fix-lruhandle-key-storage

Conversation

@qingketsing
Copy link
Copy Markdown

Potentially addresses #1325.

Fixes C++ undefined behavior in the LRU cache entry key storage.

LRUHandle used char key_data[1] as variable-length trailing storage and copied arbitrary-length keys into it(In cache.cc). For keys longer than one byte, this writes past the declared array subobject even though it remains within the malloc allocation.

This patch stores keys in tail storage after LRUHandle instead, preserving the single-allocation layout.

I do not have access to the custom HWASan runtime from the issue report, so I cannot directly verify the original sanitizer failure. The change removes the fixed trailing array pattern identified in the report and adds a cache test for keys longer than one byte.

Tested:

  • clang-format -i --style=file util/cache.cc util/cache_test.cc
  • build/tools-venv/bin/ctest --test-dir build/cmake --output-on-failure

Store cache entry keys in tail storage after LRUHandle instead of using a one-byte trailing array. Treating key_data[1] as variable-length storage and copying keys longer than one byte into it writes past the declared array subobject, which is undefined behavior in C++.

Add a cache test covering insertion, lookup, and deletion of a key longer than one byte.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant