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

commit r16-2263-ga05c4f4ee48f76e518dbd2a96e5083f4df833df7
Author: Kwok Cheung Yeung <kcye...@baylibre.com>
Date:   Tue Jul 15 15:26:26 2025 +0100

    openmp, fortran: Fix ICE when the procedure name cannot be found in declare 
variant directives [PR104428]
    
    The result of searching for the procedure name symbol should be checked in
    case the symbol cannot be found to avoid a null dereference.
    
    gcc/fortran/
    
            PR fortran/104428
            * trans-openmp.cc (gfc_trans_omp_declare_variant): Check that 
proc_st
            is non-NULL before dereferencing.  Add line number to error message.
    
    gcc/testsuite/
    
            PR fortran/104428
            * gfortran.dg/gomp/pr104428.f90: New.

Diff:
---
 gcc/fortran/trans-openmp.cc                 |  5 +++--
 gcc/testsuite/gfortran.dg/gomp/pr104428.f90 | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index f3d7cd4ffee9..278e91c2c495 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -9714,11 +9714,12 @@ gfc_trans_omp_declare_variant (gfc_namespace *ns, 
gfc_namespace *parent_ns)
        {
          gfc_symtree *proc_st;
          gfc_find_sym_tree (variant_proc_name, gfc_current_ns, 1, &proc_st);
-         variant_proc_sym = proc_st->n.sym;
+         variant_proc_sym = proc_st ? proc_st->n.sym : NULL;
        }
       if (variant_proc_sym == NULL)
        {
-         gfc_error ("Cannot find symbol %qs", variant_proc_name);
+         gfc_error ("Cannot find symbol %qs at %L", variant_proc_name,
+                                                    &odv->where);
          continue;
        }
       set_selectors = omp_check_context_selector
diff --git a/gcc/testsuite/gfortran.dg/gomp/pr104428.f90 
b/gcc/testsuite/gfortran.dg/gomp/pr104428.f90
new file mode 100644
index 000000000000..639b331ff5b6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/pr104428.f90
@@ -0,0 +1,15 @@
+! { dg-do compile }
+
+program p
+  interface
+    subroutine x
+    end subroutine x
+  end interface
+contains
+  subroutine foo
+    !$omp declare variant(x) match(construct={do})
+  end
+  subroutine bar
+    !$omp declare variant(y) match(construct={do}) ! { dg-error "Cannot find 
symbol 'y'" }
+  end
+end

Reply via email to