Commit 1eb696f
fortran: Fix ICE and self-assignment bugs with recursive allocatable finalizers [PR90519]
Derived types with recursive allocatable components and FINAL procedures
trigger an ICE in gimplify_call_expr because the finalizer wrapper's result
symbol references itself (final->result = final), creating a cycle. This
patch creates a separate __result_<typename> symbol to break the cycle.
Self-assignment (a = a) with such types causes use-after-free because the
left-hand side is finalized before copying, destroying the source. This
patch adds detection using gfc_dep_compare_expr at compile time and pointer
comparison at runtime to skip finalization when lhs == rhs.
Parenthesized self-assignment (a = (a)) creates a temporary, defeating the
simple self-assignment detection. This patch adds strip_parentheses() to
look through INTRINSIC_PARENTHESES operators and ensure deep_copy is enabled
for such cases.
Test pr112459.f90 now expects 6 _final calls instead of 12 because separate
result symbols eliminate double-counting in tree dumps.
PR fortran/90519
gcc/fortran/ChangeLog:
* trans-expr.cc (strip_parentheses): New helper function to strip
INTRINSIC_PARENTHESES operators from expressions.
(is_runtime_conformable): Use strip_parentheses to handle cases
like a = (a) when checking for self-assignment.
(gfc_trans_assignment_1): Strip parentheses before checking if
expr2 is a variable, ensuring deep_copy is enabled for cases like
a = (a). Also strip parentheses when checking for self-assignment
to avoid use-after-free in finalization.
(gfc_trans_scalar_assign): Add comment about parentheses handling.
* class.cc (generate_finalization_wrapper): Create separate result
symbol for finalizer wrapper functions instead of self-referencing
the procedure symbol, avoiding ICE in gimplify_call_expr.
gcc/testsuite/ChangeLog:
* gfortran.dg/finalizer_recursive_alloc_1.f90: New test for ICE fix.
* gfortran.dg/finalizer_recursive_alloc_2.f90: New execution test.
* gfortran.dg/finalizer_self_assign.f90: New test for self-assignment
including a = a, a = (a), and a = (((a))) cases using if/stop pattern.
* gfortran.dg/pr112459.f90: Update to expect 6 _final calls instead
of 12, reflecting corrected self-assignment behavior.
Signed-off-by: Christopher Albert <[email protected]>1 parent 5e62a23 commit 1eb696f
File tree
6 files changed
+223
-14
lines changed- gcc
- fortran
- testsuite/gfortran.dg
6 files changed
+223
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1733 | 1733 | | |
1734 | 1734 | | |
1735 | 1735 | | |
| 1736 | + | |
1736 | 1737 | | |
1737 | 1738 | | |
1738 | 1739 | | |
1739 | 1740 | | |
| 1741 | + | |
1740 | 1742 | | |
1741 | 1743 | | |
1742 | 1744 | | |
| |||
1824 | 1826 | | |
1825 | 1827 | | |
1826 | 1828 | | |
1827 | | - | |
1828 | 1829 | | |
1829 | 1830 | | |
1830 | 1831 | | |
1831 | 1832 | | |
1832 | 1833 | | |
1833 | 1834 | | |
1834 | 1835 | | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
1835 | 1856 | | |
1836 | 1857 | | |
1837 | 1858 | | |
| |||
1959 | 1980 | | |
1960 | 1981 | | |
1961 | 1982 | | |
1962 | | - | |
| 1983 | + | |
1963 | 1984 | | |
1964 | 1985 | | |
1965 | 1986 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11697 | 11697 | | |
11698 | 11698 | | |
11699 | 11699 | | |
11700 | | - | |
| 11700 | + | |
| 11701 | + | |
| 11702 | + | |
| 11703 | + | |
| 11704 | + | |
| 11705 | + | |
| 11706 | + | |
| 11707 | + | |
| 11708 | + | |
| 11709 | + | |
| 11710 | + | |
11701 | 11711 | | |
11702 | 11712 | | |
11703 | 11713 | | |
| |||
12683 | 12693 | | |
12684 | 12694 | | |
12685 | 12695 | | |
| 12696 | + | |
| 12697 | + | |
| 12698 | + | |
| 12699 | + | |
| 12700 | + | |
| 12701 | + | |
| 12702 | + | |
| 12703 | + | |
| 12704 | + | |
| 12705 | + | |
| 12706 | + | |
| 12707 | + | |
| 12708 | + | |
| 12709 | + | |
12686 | 12710 | | |
12687 | 12711 | | |
12688 | 12712 | | |
12689 | 12713 | | |
12690 | 12714 | | |
12691 | 12715 | | |
| 12716 | + | |
| 12717 | + | |
| 12718 | + | |
| 12719 | + | |
12692 | 12720 | | |
12693 | 12721 | | |
12694 | 12722 | | |
| |||
13390 | 13418 | | |
13391 | 13419 | | |
13392 | 13420 | | |
13393 | | - | |
| 13421 | + | |
| 13422 | + | |
| 13423 | + | |
13394 | 13424 | | |
13395 | | - | |
13396 | | - | |
| 13425 | + | |
| 13426 | + | |
| 13427 | + | |
| 13428 | + | |
| 13429 | + | |
13397 | 13430 | | |
13398 | 13431 | | |
13399 | 13432 | | |
| |||
13416 | 13449 | | |
13417 | 13450 | | |
13418 | 13451 | | |
13419 | | - | |
13420 | | - | |
13421 | | - | |
13422 | | - | |
13423 | | - | |
13424 | | - | |
13425 | | - | |
| 13452 | + | |
| 13453 | + | |
| 13454 | + | |
| 13455 | + | |
| 13456 | + | |
| 13457 | + | |
| 13458 | + | |
| 13459 | + | |
| 13460 | + | |
| 13461 | + | |
| 13462 | + | |
| 13463 | + | |
13426 | 13464 | | |
13427 | 13465 | | |
13428 | 13466 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
0 commit comments