Commit abf3059
committed
Fix AVX2 SIMD boundary overread in colorShuffle_avx2
This commit fixes memory overread issues in AVX2-optimized RGB/BGR color
conversions that could cause crashes at certain image widths (e.g., 1280).
Problem:
- 3->3 conversion (RGB↔BGR): Reads 16 bytes but only needs 15, causing
1-byte overread at boundaries
- 3->4 conversion (RGB→RGBA): Second read extends to offset+27, causing
4-byte overread at boundaries
Root cause:
SIMD intrinsics (_mm_loadu_si128) read fixed byte amounts that can exceed
actual pixel data at image boundaries.
Solution:
Calculate proper loop boundary for each conversion type:
- 3->4 conversions: patchSize + 2 (needs 2 extra pixels margin)
- 3->3 conversions: patchSize + 1 (needs 1 extra pixel margin)
- 4->4 and 4->3: patchSize (already safe)
Testing:
Added 78 comprehensive test cases covering:
- Issue #30 exact scenario (1280x720)
- Critical boundary widths (1272-1280, 1915-1920)
- Small/tiny widths (1-10 pixels)
- Non-contiguous memory (with stride padding)
All tests pass on both CPU and AVX2 backends.
Fixes #301 parent bed9a0e commit abf3059
3 files changed
Lines changed: 438 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
184 | 193 | | |
185 | 194 | | |
186 | 195 | | |
187 | 196 | | |
188 | | - | |
| 197 | + | |
| 198 | + | |
189 | 199 | | |
190 | 200 | | |
191 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| |||
0 commit comments