Skip to content

fix(memorytracker): correct StringTable search and entry sizing#20

Open
Rakdos8 wants to merge 1 commit into
carbonengine:mainfrom
Rakdos8:fix/memory-tracker-search-and-overflow
Open

fix(memorytracker): correct StringTable search and entry sizing#20
Rakdos8 wants to merge 1 commit into
carbonengine:mainfrom
Rakdos8:fix/memory-tracker-search-and-overflow

Conversation

@Rakdos8
Copy link
Copy Markdown

@Rakdos8 Rakdos8 commented May 15, 2026

Summary

Fixes a guaranteed-wrong binary search and an arena buffer overflow in the
memory tracker's string table.

Problem

  1. StringTable::Find walked an m_id-ascending table with its comparison
    branches inverted: present ids were not found, duplicates were inserted,
    sort order broke, and LookUp returned garbage.
  2. AddString sized the StringEntry block with sizeof(TableEntry) (the
    wrong struct), omitting the StringEntry header, so the string was
    written past the reserved block and corrupted neighbouring entries in
    the fixed 512 KB arena.

Fix

Swap the search branches and set ix to the insertion point on miss.
Size the entry from offsetof(StringEntry, m_string) and pass the matching
available length to strcpy_s. Adds #include <cstddef> for offsetof.

Type

Correctness + memory corruption (Critical for a foundational lib).

Testing

Manual review; search now lower-bound correct, allocation accounts for the
full variable-length-struct header.

StringTable::Find walked a m_id-ascending table with its comparison
branches inverted, so present ids were not found, duplicates were
inserted and the table's sort order broke (LookUp returned garbage).
Swap the branches and set ix to the insertion point on miss.

AddString sized the StringEntry block with sizeof(TableEntry) (the
wrong struct), omitting the StringEntry header, so the string was
written past the reserved block and corrupted neighbouring entries in
the fixed arena. Size from offsetof(StringEntry, m_string) and pass the
matching available length to strcpy_s.
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