Jelajahi Sumber

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

git-svn-id: trunk@48449 -
florian 4 tahun lalu
induk
melakukan
e99827246e
3 mengubah file dengan 23 tambahan dan 1 penghapusan
  1. 1 0
      .gitattributes
  2. 10 1
      compiler/nmem.pas
  3. 12 0
      tests/webtbs/tw38413.pp

+ 1 - 0
.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

+ 10 - 1
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,

+ 12 - 0
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.