From dcac6b9c6f623df5cba90d97629b551e1adf0bad Mon Sep 17 00:00:00 2001 From: Jonas Maebe Date: Sun, 31 Dec 2017 15:18:40 +0000 Subject: [PATCH] * disallow passing a dynarray(niln/pointerconstn) as an open array (mantis #31273) git-svn-id: trunk@37885 - --- .gitattributes | 2 ++ compiler/ncnv.pas | 6 +++++- tests/webtbf/tw31273.pp | 18 ++++++++++++++++++ tests/webtbs/tw31273a.pp | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/webtbf/tw31273.pp create mode 100644 tests/webtbs/tw31273a.pp diff --git a/.gitattributes b/.gitattributes index 4ecd152889..8de92db267 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14296,6 +14296,7 @@ tests/webtbf/tw31107.pp svneol=native#text/pascal tests/webtbf/tw3114.pp svneol=native#text/plain tests/webtbf/tw3116.pp svneol=native#text/plain tests/webtbf/tw3126.pp svneol=native#text/plain +tests/webtbf/tw31273.pp svneol=native#text/plain tests/webtbf/tw3145.pp svneol=native#text/plain tests/webtbf/tw31465.pp svneol=native#text/pascal tests/webtbf/tw3183.pp svneol=native#text/plain @@ -15854,6 +15855,7 @@ tests/webtbs/tw31165.pp svneol=native#text/pascal tests/webtbs/tw31201.pp svneol=native#text/pascal tests/webtbs/tw3124.pp svneol=native#text/plain tests/webtbs/tw31246.pp svneol=native#text/pascal +tests/webtbs/tw31273a.pp svneol=native#text/plain tests/webtbs/tw31305.pp svneol=native#text/pascal tests/webtbs/tw3131.pp svneol=native#text/plain tests/webtbs/tw31332.pp svneol=native#text/pascal diff --git a/compiler/ncnv.pas b/compiler/ncnv.pas index b7e704ebe1..8eb2246778 100644 --- a/compiler/ncnv.pas +++ b/compiler/ncnv.pas @@ -1704,6 +1704,8 @@ implementation function ttypeconvnode.typecheck_dynarray_to_openarray : tnode; begin + if (actualtargetnode(@left)^.nodetype in [pointerconstn,niln]) then + CGMessage(type_e_no_addr_of_constant); { a dynamic array is a pointer to an array, so to convert it to } { an open array, we have to dereference it (JM) } result := ctypeconvnode.create_internal(left,cpointerdef.getreusable(resultdef)); @@ -2913,7 +2915,9 @@ implementation methodpointer. The typeconv of the methodpointer will then take care of updateing size of niln to OS_64 } if not((resultdef.typ=procvardef) and - not(tprocvardef(resultdef).is_addressonly)) then + not(tprocvardef(resultdef).is_addressonly)) and + { converting (dynamic array) nil to a an open array is not allowed } + not is_open_array(resultdef) then begin left.resultdef:=resultdef; if ([nf_explicit,nf_internal] * flags <> []) then diff --git a/tests/webtbf/tw31273.pp b/tests/webtbf/tw31273.pp new file mode 100644 index 0000000000..1d8e89e7a9 --- /dev/null +++ b/tests/webtbf/tw31273.pp @@ -0,0 +1,18 @@ +{ %fail } + +{$mode delphi} + +program Project1; +{$APPTYPE CONSOLE} + +function SumX(const Arr: array of SizeInt): Integer; +begin + Result := Arr[Low(Arr)]; +end; + +var + P: Pointer; +begin + P := nil; + Writeln(SumX(TBoundArray(nil))); // Case 2 +end. diff --git a/tests/webtbs/tw31273a.pp b/tests/webtbs/tw31273a.pp new file mode 100644 index 0000000000..b0df47a887 --- /dev/null +++ b/tests/webtbs/tw31273a.pp @@ -0,0 +1,18 @@ +{$mode delphi} + +program Project1; +{$APPTYPE CONSOLE} + +function SumX(const Arr: array of SizeInt): Integer; +begin + if high(arr)<>-1 then + halt(1); + result:=1; +end; + +var + P: Pointer; +begin + P := nil; + Writeln(SumX(TBoundArray(P))); // Case 1 +end.