Kwok Cheung Yeung wrote:
This patch fixes an ICE due to a null-pointer dereference when finding the symbol for the procedure name in a declare variant directive, which occurs because the result of gfc_find_sym_tree is being dereferenced unconditionally. The result is now checked, and the symbol is set to NULL if it can't be found, resulting in a subsequent error.

If the symbol is implicitly typed, then the error will also occur. I think this makes sense as implicit variables are of type integer or real, which cannot be used to specify the variant procedure.

The latter sounds wrong – but fortunately, there seems to be no issue, if I 
look at:

program p
  interface
    function x()
    end function x
  end interface
  print *, foo()
  !$omp do
  do i = 1, 1
    print *, foo()
  end do
contains
  function foo()
    !$omp declare variant(x) match(construct={do})
  end
end

which compiles just fine. (The functions should be implicitly real.)

Tested on x86_64 host, okay for trunk?

LGTM, including backporting. (At least backporting to GCC 15 seems to make sense; it is marked as [13/14/15/16 Regression], but was reported against GCC 12 in Feb 2022.)

Minor comments:

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.

Thanks for using the location data for the error message!


--- a/gcc/fortran/trans-openmp.cc
…
-         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,
+

--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/pr104428.f90
@@ -0,0 +1,16 @@
+! { dg-do compile }
+! { dg-options "-fopenmp" }

This shouldn't be needed for gomp/

…

+    !$omp declare variant(y) match(construct={do}) ! { dg-error "Cannot find symbol 
.y." }

GCC is run such that 'y' works – i.e. not printing some fancy quotation marks like ‘…’ or “…” or «…» (and contrary to "(1)", also no \\ escaping is required).

Thanks for the patch!

Tobias

Reply via email to