Skip to content

Fix potential OOM/panic in Apply#75

Merged
bluekeyes merged 1 commit into
masterfrom
fix-apply-alloc
Jul 18, 2026
Merged

Fix potential OOM/panic in Apply#75
bluekeyes merged 1 commit into
masterfrom
fix-apply-alloc

Conversation

@bluekeyes

Copy link
Copy Markdown
Owner

The apply logic trusted the line counts of the incoming patch when allocating memory for preimages. This meant a patch could specify a very large old position in a fragment and force the library to allocate excessive memory before checking if the source actually contains that many lines.

To fix this while keeping the current structure that reads the source incrementally, read the preimage in chunks of 4096 lines. This bounds the memory that might be allocated before we reach the end of the source file. Most practical patches should only require one or two reads.

Also fix a related panic that could happen with an old position close to math.MaxInt64.

The apply logic trusted the line counts of the incoming patch when
allocating memory for preimages. This meant a patch could specify a very
large old position in a fragment and force the library to allocate
excessive memory before checking if the source actually contains that
many lines.

To fix this while keeping the current structure that reads the source
incrementally, read the preimage in chunks of 4096 lines. This bounds
the memory that might be allocated before we reach the end of the source
file. Most practical patches should only require one or two reads.

Also fix a related panic that could happen with an old position close to
math.MaxInt64.
@bluekeyes
bluekeyes merged commit 706d29d into master Jul 18, 2026
1 check passed
@bluekeyes
bluekeyes deleted the fix-apply-alloc branch July 18, 2026 17:28
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