Repository navigation
history: survive allocation failure, and bound the stored deltas - #3116
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
history_addXORed the last committed state in place before allocating the step: a failedmalloccrashed on the NULL copy and left the history corrupted. The create path and the list insertion never checkedmallocat all.The module keeps the same public API and XOR-delta format, with:
history_createcleans up and returns NULL, and all entry points tolerate NULL;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 throughHISTORY_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