robobun
9907c2e9fa
fix(patch): add bounds checking to prevent segfault during patch application (#21939)
## Summary
- Fixes segmentation fault when applying patches with out-of-bounds line
numbers
- Adds comprehensive bounds checking in patch application logic
- Includes regression tests to prevent future issues
## Problem
Previously, malformed patches with line numbers beyond file bounds could
cause segmentation faults by attempting to access memory beyond
allocated array bounds in `addManyAt()` and `replaceRange()` calls.
## Solution
Added bounds validation at four key points in `src/patch.zig`:
1. **Hunk start position validation** (line 283-286) - Ensures hunk
starts within file bounds
2. **Context line validation** (line 294-297) - Validates context lines
exist within bounds
3. **Insertion position validation** (line 302-305) - Checks insertion
position is valid
4. **Deletion range validation** (line 317-320) - Ensures deletion range
is within bounds
All bounds violations now return `EINVAL` error gracefully instead of
crashing.
## Test Coverage
Added comprehensive regression tests in
`test/regression/issue/patch-bounds-check.test.ts`:
- ✅ Out-of-bounds insertion attempts
- ✅ Out-of-bounds deletion attempts
- ✅ Out-of-bounds context line validation
- ✅ Valid patch application (positive test case)
Tests verify that `bun install` completes gracefully when encountering
malformed patches, with no crashes or memory corruption.
## Test Results
```
bun test v1.2.21
✅ Bounds checking working: bun install completed gracefully despite malformed patch
✅ Bounds checking working: bun install completed gracefully despite deletion beyond bounds
✅ Bounds checking working: bun install completed gracefully despite context lines beyond bounds
4 pass
0 fail
22 expect() calls
Ran 4 tests across 1 file. [4.70s]
```
🤖 Generated with [Claude Code](https://claude.ai/code)
---------
Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Co-authored-by: Zack Radisic <56137411+zackradisic@users.noreply.github.com>
2025-09-12 23:44:48 -07:00
..
2025-09-09 21:42:01 -07:00
2025-01-26 04:29:54 -08:00
2025-08-11 16:26:03 -07:00
2025-07-28 12:29:47 -07:00
2025-08-12 17:07:46 -07:00
2024-09-11 16:47:38 -07:00
2024-09-03 21:32:52 -07:00
2023-03-07 12:22:34 -08:00
2023-03-07 12:22:34 -08:00
2023-03-07 12:22:34 -08:00
2023-10-10 20:05:58 -07:00
2025-09-10 17:36:23 -07:00
2025-08-14 16:36:44 -07:00
2025-07-14 00:47:53 -07:00
2025-07-14 00:47:53 -07:00
2024-09-03 21:32:52 -07:00
2025-07-25 16:17:50 -07:00
2025-09-12 23:44:48 -07:00
2024-09-03 21:32:52 -07:00
2025-05-20 21:11:22 -07:00
2025-08-05 05:12:22 -07:00
2025-07-14 00:47:53 -07:00
2025-09-09 21:42:01 -07:00
2025-07-17 05:41:06 -07:00
2025-07-14 00:47:53 -07:00
2025-07-16 01:01:10 -07:00
2025-07-14 00:47:53 -07:00
2025-07-15 01:14:58 -07:00
2024-09-03 21:32:52 -07:00
2025-07-15 22:14:00 -07:00
2025-09-09 21:42:01 -07:00
2025-07-07 11:21:36 -07:00
2025-07-16 02:00:53 -07:00
2025-07-14 00:47:53 -07:00
2025-07-14 00:47:53 -07:00
2025-07-14 00:47:53 -07:00
2024-09-03 21:32:52 -07:00
2024-09-03 21:32:52 -07:00
2025-08-27 06:39:11 -07:00
2025-08-27 06:39:11 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-08-05 05:12:22 -07:00
2025-07-14 00:47:53 -07:00
2025-07-16 01:01:10 -07:00
2025-01-20 23:17:52 -08:00
2025-08-12 17:07:46 -07:00
2025-07-15 22:14:27 -07:00
2025-08-21 14:53:50 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-07-28 12:29:47 -07:00
2025-09-09 21:42:01 -07:00
2023-10-12 15:17:03 -07:00
2025-08-12 22:19:10 -07:00
2025-01-03 08:21:00 -08:00
2025-08-21 14:53:50 -07:00
2023-03-29 20:38:19 -07:00
2023-10-13 20:37:48 -07:00
2025-08-12 17:07:46 -07:00
2025-08-12 17:07:46 -07:00
2025-07-14 00:47:53 -07:00
2025-07-14 00:47:53 -07:00
2023-10-10 20:05:58 -07:00
2023-10-10 20:05:58 -07:00
2024-10-12 02:37:51 -07:00
2024-06-12 16:50:57 -07:00
2023-12-11 22:08:25 -08:00
2025-05-08 23:22:16 -07:00
2025-08-12 17:07:46 -07:00
2024-03-06 20:38:49 -08:00
2024-05-27 02:17:43 -07:00
2025-09-09 21:42:01 -07:00
2025-07-16 01:01:10 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2025-09-09 21:42:01 -07:00
2024-02-19 22:50:24 -08:00