Commit cbc22bf
fix: rotate-by-zero UB in GPU/DSP ROR/RORQ opcodes
UBSAN on PR #126 found:
src/tom/gpu.c:1674: shift exponent 32 too large for uint32_t
The classic rotate-right idiom
(RN >> r1) | (RN << (32 - r1))
invokes UB when r1 == 0 (32-bit shift count of 32 is out of range).
Replace with the standard portable pattern:
(RN >> r1) | (RN << ((-r1) & 31))
which yields RN | RN == RN for r1 == 0 and is identical for
r1 in [1, 31]. Modern GCC/clang pattern-match this idiom into a
single rotate instruction (rorl on x86, ror on ARM).
Six sites across two files:
- src/tom/gpu.c gpu_opcode_ror, gpu_opcode_rorq
- src/jerry/dsp.c dsp_opcode_ror, dsp_opcode_rorq, plus the
prefetched (PRN) variants
Co-Authored-By: Claude Opus 4.7 <[email protected]>1 parent da531e5 commit cbc22bf
2 files changed
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1409 | 1409 | | |
1410 | 1410 | | |
1411 | 1411 | | |
1412 | | - | |
| 1412 | + | |
1413 | 1413 | | |
1414 | 1414 | | |
1415 | 1415 | | |
| |||
1419 | 1419 | | |
1420 | 1420 | | |
1421 | 1421 | | |
1422 | | - | |
| 1422 | + | |
1423 | 1423 | | |
1424 | 1424 | | |
1425 | 1425 | | |
| |||
2226 | 2226 | | |
2227 | 2227 | | |
2228 | 2228 | | |
2229 | | - | |
| 2229 | + | |
2230 | 2230 | | |
2231 | 2231 | | |
2232 | 2232 | | |
| |||
2235 | 2235 | | |
2236 | 2236 | | |
2237 | 2237 | | |
2238 | | - | |
| 2238 | + | |
2239 | 2239 | | |
2240 | 2240 | | |
2241 | 2241 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1671 | 1671 | | |
1672 | 1672 | | |
1673 | 1673 | | |
1674 | | - | |
| 1674 | + | |
1675 | 1675 | | |
1676 | 1676 | | |
1677 | 1677 | | |
| |||
1681 | 1681 | | |
1682 | 1682 | | |
1683 | 1683 | | |
1684 | | - | |
| 1684 | + | |
1685 | 1685 | | |
1686 | 1686 | | |
1687 | 1687 | | |
| |||
0 commit comments