Skip to content

history: survive allocation failure, and bound the stored deltas - #3116

Merged
nesbox merged 3 commits into
mainfrom
history-review
Oct 23, 2026
Merged

nesbox merged 3 commits into
mainfrom
history-review

Conversation

@nesbox

@nesbox nesbox commented Oct 23, 2026

Copy link
Copy Markdown
Owner

What

history_add XORed the last committed state in place before allocating the step: a failed malloc crashed on the NULL copy and left the history corrupted. The create path and the list insertion never checked malloc at all.

The module keeps the same public API and XOR-delta format, with:

  • the delta computed into its own buffer before any state changes; a failed allocation returns false and leaves the history consistent;
  • every allocation checked, history_create cleans up and returns NULL, and all entry points tolerate NULL;
  • only the changed span XORed instead of a whole-buffer XOR and restore per commit;
  • the stored deltas capped at 64 MiB (HISTORY_MAX_BYTES), dropping the oldest steps first; the current step is always kept.

Undo depth is now bounded, which is the one behaviour change: a long session in the code editor (a 1 MiB state) can no longer grow the history without limit.

Tests

  • tests/history.c (history-test): no-op commits, buffer edges, ordered undo/redo with bounds, branch replacement including from the oldest step, uncommitted edits, zero/one-byte buffers, NULL calls, failed allocations through HISTORY_TEST_ALLOC (cleanup, no-step, consistency), 200k randomized commit/undo/redo operations checked against full snapshots, and allocation-count leak checks.
  • tests/history_limit.c (history-limit-test, a 25-byte budget): eviction order, undo stopping at the oldest retained step, an oversized current step kept, and redo-branch bytes returned before the budget check.

Verification

ctest -R history, UBSan builds of both targets, the sokol studio build, and clang-format 20.1.6.

🤖 Generated with Claude Code

nesbox and others added 3 commits October 23, 2026 18:35
working_dir replaces the cd prefix in every command, fresh keeps
Release, and the make variant is now ninja. The static toggles and
BUILD_PRO=OFF go: they only restate the defaults. tests builds
build-tests the way CI does; conformance runs just the API table there.

Co-Authored-By: Claude Code <noreply@anthropic.com>
The module kept the XOR delta in the committed state while it allocated the
step, so a failed malloc left the history corrupted and the copy crashed; the
create path and the list insertion never checked malloc at all.

Compute the delta into its own buffer before touching the state, return false
and leave the history consistent when an allocation fails, clean up in
history_create and return NULL, and let every entry point take NULL. The delta
covers the changed span only: two comparisons find it and only that span is
XORed, instead of a whole-buffer XOR and restore per commit.

Depth stays unbounded, as before: a cap would change the editors' undo depth
and is a decision of its own.

tests/history.c pins the API, drives 200k random commit/undo/redo ops against
full snapshots, and fails allocations through HISTORY_TEST_ALLOC to check the
cleanup and no-step paths. history-test is registered with ctest.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Depth was unbounded: a long session in the code editor, whose state is 1 MiB,
could hold hundreds of megabytes of XOR deltas. Keep the sum of stored deltas
under HISTORY_MAX_BYTES and drop the oldest steps once a commit passes it; the
current step is always kept, even when it exceeds the budget alone.

tests/history_limit.c builds the module against a 25-byte budget so eviction
fits in a few commits, and checks that undo stops at the oldest retained step,
that an oversized step survives, and that a replaced redo branch gives its
bytes back before the budget is checked.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@nesbox
nesbox merged commit 4f28072 into main Oct 23, 2026
17 checks passed
@nesbox
nesbox deleted the history-review branch October 23, 2026 17:39
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