https://gcc.gnu.org/g:fa13241d67e21088305a6fc54c76b9f9a3d9d4c4

commit fa13241d67e21088305a6fc54c76b9f9a3d9d4c4
Author: Tobias Burnus <tbur...@baylibre.com>
Date:   Mon Mar 31 11:44:26 2025 +0200

    OpenMP: modify_call_for_omp_dispatch - fix invalid memory access after 
'error' [PR119541]
    
    OpenMP requires that the number of dispatch 'interop' clauses (ninterop)
    is less or equal to the number of declare variant 'append_args' interop
    objects (nappend).
    
    While 'nappend < ninterop' was diagnosed as error, the processing continues,
    which lead to an invalid out-of-bounds memory access. Solution: only
    process the first nappend 'interop' clauses.
    
    gcc/ChangeLog:
    
            PR middle-end/119541
            * gimplify.cc (modify_call_for_omp_dispatch): Limit interop claues
            processing by the number of append_args arguments.
    
    (cherry picked from commit f3899e0fd3f9aa6b579a21e87b50c61ea5c448df)

Diff:
---
 gcc/ChangeLog.omp           | 9 +++++++++
 gcc/gimplify.cc             | 3 ++-
 gcc/testsuite/ChangeLog.omp | 8 ++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp
index 3e349ac21873..5287d5333895 100644
--- a/gcc/ChangeLog.omp
+++ b/gcc/ChangeLog.omp
@@ -1,3 +1,12 @@
+2025-03-31  Tobias Burnus  <tbur...@baylibre.com>
+
+       Backported from master:
+       2025-03-31  Tobias Burnus  <tbur...@baylibre.com>
+
+       PR middle-end/119541
+       * gimplify.cc (modify_call_for_omp_dispatch): Limit interop claues
+       processing by the number of append_args arguments.
+
 2025-03-27  Tobias Burnus  <tbur...@baylibre.com>
 
        Backported from master:
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 49ff5ace9bcf..f85a722e2d5c 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -3969,6 +3969,7 @@ modify_call_for_omp_dispatch (tree expr, tree 
dispatch_clauses,
                  : DECL_SOURCE_LOCATION (fndecl),
                  "%<declare variant%> candidate %qD declared here",
                  fndecl);
+         ninterop = nappend;
        }
     }
   if (dispatch_interop && !dispatch_device_num)
@@ -3991,7 +3992,7 @@ modify_call_for_omp_dispatch (tree expr, tree 
dispatch_clauses,
          buffer[i] = CALL_EXPR_ARG (expr, i);
        }
       int j = ninterop;
-      for (tree t = dispatch_interop; t; t = TREE_CHAIN (t))
+      for (tree t = dispatch_interop; t && j > 0; t = TREE_CHAIN (t))
        if (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_INTEROP)
          buffer[i + --j] = OMP_CLAUSE_DECL (t);
       gcc_checking_assert (j == 0);
diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp
index 675f1ba3792d..0c39103e3b33 100644
--- a/gcc/testsuite/ChangeLog.omp
+++ b/gcc/testsuite/ChangeLog.omp
@@ -1,3 +1,11 @@
+2025-03-30  Tobias Burnus  <tbur...@baylibre.com>
+
+       Backported from master:
+       2025-03-30  Tobias Burnus  <tbur...@baylibre.com>
+
+       * g++.dg/gomp/append-args-8.C: Remove bogus '3' after \.\[0-9\]+
+       pattern.
+
 2025-03-27  Tobias Burnus  <tbur...@baylibre.com>
 
        Backported from master:

Reply via email to