From e99827246e52288205bf3dff3533634aa553acb3 Mon Sep 17 00:00:00 2001 From: florian Date: Thu, 28 Jan 2021 20:56:15 +0000 Subject: [PATCH] * fix type conversion for array indicies if the ordinal ranges of the involved types do not overlap, resolves #38413 git-svn-id: trunk@48449 - --- .gitattributes | 1 + compiler/nmem.pas | 11 ++++++++++- tests/webtbs/tw38413.pp | 12 ++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/webtbs/tw38413.pp diff --git a/.gitattributes b/.gitattributes index e97e53ec58..3973a35c2e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/compiler/nmem.pas b/compiler/nmem.pas index c5920e5741..2f2851f249 100644 --- a/compiler/nmem.pas +++ b/compiler/nmem.pas @@ -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, diff --git a/tests/webtbs/tw38413.pp b/tests/webtbs/tw38413.pp new file mode 100644 index 0000000000..ccd5930eac --- /dev/null +++ b/tests/webtbs/tw38413.pp @@ -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.