* fix type conversion for array indicies if the ordinal ranges of the involved types do not overlap, resolves #38413

git-svn-id: trunk@48449 -
This commit is contained in:
florian 2021-01-28 20:56:15 +00:00
parent 89149a2f9f
commit e99827246e
3 changed files with 23 additions and 1 deletions

1
.gitattributes vendored
View File

@ -18649,6 +18649,7 @@ tests/webtbs/tw38385.pp svneol=native#text/pascal
tests/webtbs/tw38390.pp svneol=native#text/pascal
tests/webtbs/tw3840.pp svneol=native#text/plain
tests/webtbs/tw3841.pp svneol=native#text/plain
tests/webtbs/tw38413.pp svneol=native#text/pascal
tests/webtbs/tw3863.pp svneol=native#text/plain
tests/webtbs/tw3864.pp svneol=native#text/plain
tests/webtbs/tw3865.pp svneol=native#text/plain

View File

@ -1051,7 +1051,16 @@ implementation
and not is_64bit(right.resultdef)
{$endif not cpu64bitaddr}
then
newordtyp:=Torddef(right.resultdef).ordtype
begin
{ in case of an integer type, we need a new type which covers declaration range and index range,
see tests/webtbs/tw38413.pp
}
if is_integer(right.resultdef) then
newordtyp:=range_to_basetype(min(TConstExprInt(Tarraydef(left.resultdef).lowrange),torddef(right.resultdef).low),
max(TConstExprInt(Tarraydef(left.resultdef).highrange),torddef(right.resultdef).high))
else
newordtyp:=Torddef(right.resultdef).ordtype;
end
else
newordtyp:=torddef(sizesinttype).ordtype;
inserttypeconv(right,corddef.create(newordtyp,

12
tests/webtbs/tw38413.pp Normal file
View File

@ -0,0 +1,12 @@
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
if value<>4 then
halt(1);
writeln('ok');
end.