Wrong address calculation when indexing an array with negative low bound
Original Reporter info from Mantis: Cooler
-
Reporter name: Ivan Polyacov
Original Reporter info from Mantis: Cooler
- Reporter name: Ivan Polyacov
Description:
When array's low bound is negative (like array[-1..X]) and variable used to index it is unsigned (like byte), it is treated as signed when calculating target address.
This cause access to wrong memory and may lead to data damage, access violation etc.
This happens in both 32-bit and 64-bit modes, reproduced on Windows and Linux.
Steps to reproduce:
var
arr : array[-1..140] of byte=(4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
index , value : byte; // unsigned byte - important
begin
index:=133; // positive value, which is treated as negative
value:=arr[index]; // wrong value! Memory access outside array
Mantis conversion info:
- Mantis ID: 38413
- OS: Linux Mint
- OS Build: 20.0
- Build: r48130
- Platform: Linux
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 48449 (#e9982724)
- Monitored by: » Cooler (Ivan Polyacov)