[PATCH] fortran: Fix UBSAN error member access within null pointer (PR125860)

Martin Jambor <[email protected]> Wed, 17 Jun 2026 18:01:58 +0200
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
Hi,

with UBSAN instrumented compiler, compiling the testcase
gfortran.dg/altreturn_5.f90 at -O3 fails with:

  /home/worker/buildworker/ubsan/build/gcc/fortran/interface.cc:4717:27: runtime error: member access within null pointer of type 'struct gfc_expr'

This patch adds the necessary guard to avoid passing an invalid LOC to
gfc_value_set_and_used even when the EXPR parameter is NULL and the
function will just return.  I have left the NULL check in
gfc_value_set_and_used intact so that the behavior of the function is
consistent with gfc_value_used_expr (and possibly other functions) but
it seems like it could be removed.

Bootstrapped and tested on x86_64-linux, I have also verified the UBSAN
failure is gone with this patch.

OK for master?

Thanks,

Martin


gcc/fortran/ChangeLog:

2026-06-16  Martin Jambor  <[email protected]>

	PR fortran/125860
	* interface.cc (gfc_procedure_use): Check a-> expr is not NULL before
	calling gfc_value_set_and_used.
---
 gcc/fortran/interface.cc | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index ad688569812..58308aec9da 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -4714,8 +4714,9 @@ gfc_procedure_use (gfc_symbol *sym, gfc_actual_arglist **ap, locus *where)
 
       if (implicit)
 	for (a = *ap; a; a = a->next)
-	  gfc_value_set_and_used (a->expr, &a->expr->where, VALUE_ARG,
-				  VALUE_MAYBE_USED);
+	  if (a->expr)
+	    gfc_value_set_and_used (a->expr, &a->expr->where, VALUE_ARG,
+				    VALUE_MAYBE_USED);
 
       return true;
     }
-- 
2.54.0