From: Artur Pietrek <[email protected]>

Make_Address_For_Finalize emits a -Descriptor_Size shift whenever
the array's first subtype is unconstrained, but does not check whether
the object has a controlled component. For arrays of protected types
Has_Controlled_Component is False so no dope vector is allocated, but
at scope exit the incorrect finalize address is dereferenced, which
may lead to EXCEPTION_ACCESS_VIOLATION, or silent error.

gcc/ada/ChangeLog:

        * exp_ch7.adb (Make_Address_For_Finalize): check
        Has_Controlled_Component predicate before emitting the shift to be
        consistant with what Is_Constr_Array_Subt_With_Bounds says.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch7.adb | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb
index c303a6145df..11c86895aa1 100644
--- a/gcc/ada/exp_ch7.adb
+++ b/gcc/ada/exp_ch7.adb
@@ -5471,11 +5471,12 @@ package body Exp_Ch7 is
       --  subtype, its Finalize_Address primitive expects the address of
       --  an object with a dope vector (see Make_Finalize_Address_Stmts).
       --  This is achieved by setting Is_Constr_Array_Subt_With_Bounds,
-      --  but the address of the object is still that of its elements,
-      --  so we need to shift it back to skip the dope vector.
+      --  whose predicate also requires Has_Controlled_Component, so we
+      --  have to check for the controlled component here, too.
 
       if Is_Array_Type (Utyp)
         and then not Is_Constrained (First_Subtype (Utyp))
+        and then Has_Controlled_Component (Utyp)
       then
          Obj_Addr :=
            Shift_Address_For_Descriptor
-- 
2.53.0

Reply via email to