# Signed overflow negative offset decoder

Signed size/coordinate arithmetic that can wrap negative before indexing, clipping or memcpy in native decoders

_Area: Memory corruption and native code. Seen in 3 disclosed reports; latest 2026-11-17. Updated 2026-11-24._

## How it works
A decoder computes a position or size with signed arithmetic, then uses it as an index, offset or length. The code assumes the value stays non-negative and within bounds, but a crafted input drives the arithmetic to wrap: a large run-length count added to a position goes past INT_MAX and becomes negative, or a subtraction of two extreme coordinates wraps. The bound or clip check is then evaluated on the wrapped value, so it passes, and the negative result reaches a memory operation (index, offset, memcpy length). The clamp that was meant to protect the buffer becomes the bypass [E1](https://bugbountylibrary.com/r/h1%3A816637.md)[E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md).

## Where it shows up
- Signed accumulators (run-length counts, symbol counts) added to a position, then compared to a bound [E1](https://bugbountylibrary.com/r/h1%3A816637.md)
- Size computed as `x1 - x0` and clipping done as `x0 + size` in signed int [E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md)
- Coordinates or lengths near INT_MIN/INT_MAX reaching a `pixelsize`-multiplied offset [E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md)
- Hand-written SIMD/assembly fast paths where tail or padding handling computes an output length [E3](https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md)
- Any negative value later used as a length or offset in memcpy/indexing

## How to review
Check the exact invariant: every value used as an index, offset or length must be proven non-negative and within the allocation *after* all arithmetic, not before. For each signed addition/subtraction feeding a bound check, ask whether it can wrap. Verify clip checks use the same arithmetic domain as the final memory operation, and that a wrapped size cannot equal the source dimension. In SIMD tails, confirm padding characters cannot produce a negative length that is treated as a full-width write [E3](https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md).

## How finders spotted it
- Read compress.c and noticed `x` and `count` are signed long, the bound check is an unchecked signed addition, and Huffman symbol counts accumulate without limit [E1](https://bugbountylibrary.com/r/h1%3A816637.md)
- Examined ImagingPaste's signed int size and clip arithmetic, saw `dx1 - dx0` and `dx0 + xsize` can wrap near INT_MIN/INT_MAX, then traced the wrapped `dx` into the pixelsize-multiplied memcpy [E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md)
- Used AI agents to hunt the OpenJDK codebase, focusing on the hand-rolled assembly Base64 tail/padding path [E3](https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md)

## Variants seen
- Huffman/FAX decoder: repeated MW/MB/EX symbols push count near INT_MAX, x+count wraps negative, bypassing the columns bound [E1](https://bugbountylibrary.com/r/h1%3A816637.md)
- Image paste/crop: wrapped size and clip checks pass, then negative destination offset gives a backward heap underwrite of 4*W bytes [E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md)
- Base64 AVX-512 decoder: stray '=' padding computes negative output length treated as a 64-byte write, overflowing a 50-byte buffer [E3](https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md)

## Impact and severity
Heap out-of-bounds write; in ImageMagick, color toggling plus the line-568 clamp let the attacker skip uncontrolled writes and re-enter the TWId/TBId branch for fine-grained single-byte writes into heap metadata [E1](https://bugbountylibrary.com/r/h1%3A816637.md). Pillow's primitive scales with source width W, offset and length both 4*W, up to ~2 GiB backwards, limited by allocation and heap layout [E2](https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md). Java4Shell chained the corruption into full RCE across default-config products, including a client-side variant where a Java program connecting to a malicious TLS server is compromised without exposing any service [E3](https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md).

## Seen in real software
Open-source packages and projects, and products, where this pattern was found. If the target runs one, compare the version it runs with the fix.
- **OpenJDK** (Base64 decoder (AVX-512 VBMI assembly path)) OpenJDK 17: Java4Shell: Write once, RCE everywhere (CVE-2026-114938). Report: https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md
- **Pillow (pip)** < 12.3.0, fixed in 12.3.0: Pillow: Heap out-of-bounds write `Image.paste()` / `Image.crop()` via signed coordinate overflow (CVE-2026-59199). Report: https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md. Fix: https://github.com/python-pillow/Pillow/commit/ceefc348eb3c3844c7f9796ef2cc3a7dd5fbba7b

## Evidence
- E1: https://bugbountylibrary.com/r/h1%3A816637.md
- E2: https://bugbountylibrary.com/r/ghsa%3AGHSA-6r8x-57c9-28j4.md
- E3: https://bugbountylibrary.com/r/blog%3Acalif-io-java4shell.md
