Commit 6a1e045
[refactor] overflow protection in MemBIO buffer reallocation
The old realloc() doubled buffer.length without overflow checks, which
could wrap int to a negative value when the buffer exceeded ~1 billion
bytes. The write() loop condition (wpointer + len) could also overflow,
skipping the realloc entirely.
Replace realloc() with growTo(minCapacity) using long arithmetic to
prevent int overflow when doubling, capped at Integer.MAX_VALUE - 8.
Compute required capacity in write() with long to detect overflow before
growing. Guard against non-positive len, matching C OpenSSL's mem_write.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent e003210 commit 6a1e045
1 file changed
Lines changed: 19 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
37 | 40 | | |
38 | 41 | | |
39 | 42 | | |
40 | 43 | | |
41 | | - | |
42 | | - | |
43 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
44 | 52 | | |
45 | 53 | | |
46 | 54 | | |
| |||
75 | 83 | | |
76 | 84 | | |
77 | 85 | | |
78 | | - | |
79 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
80 | 94 | | |
81 | 95 | | |
82 | 96 | | |
| |||
0 commit comments