diff mbox series

[pushed] Fortran: fix ICE with use with rename of namelist member [PR116530]

Message ID trinity-7fad4bbf-80ff-43f8-9d70-180fce9ada2a-1724959897540@3c-app-gmx-bs45
State New
Headers show
Series [pushed] Fortran: fix ICE with use with rename of namelist member [PR116530] | expand

Commit Message

Harald Anlauf Aug. 29, 2024, 7:31 p.m. UTC
Dear all,

the attached simple & obvious patch fixes a NULL pointer dereference
when USEing with rename a namelist member and reading/writing the
namelist.

Patch was OK'ed in the PR by Steve and is pushed to mainline as:

r15-3308-g6bfeba12c86b4d0dae27d99b484f64774dd49398

As this is a 14/15 regression, I plan to backport when it has
successfully passed the testers.

Thanks,
Harald

Comments

Steve Kargl Aug. 29, 2024, 7:53 p.m. UTC | #1
Thanks for the patch.  If you have not already opened a new PR for the
other issue with C8107, I'll open one later today.  It's likely that
we need to check the namelist-group-name for USE association in
match.cc:gfc_match_namelist.

Hmmm, it seems we already catch the error, but accept it as an
extension.  

% gfcx -o z -std=f2023 a.f90 && ./z
a.f90:11:23:

   11 |    namelist /nam_nml1/j
      |                       1
Error: GNU Extension: Namelist group name 'nam_nml1' at (1) already
is USE associated and cannot be respecified.


In hindsight, I wish GNU extensions had warnings associated with them.
Harald Anlauf Aug. 29, 2024, 8:05 p.m. UTC | #2
Hi Steve,

Am 29.08.24 um 21:53 schrieb Steve Kargl:
> Thanks for the patch.  If you have not already opened a new PR for the
> other issue with C8107, I'll open one later today.  It's likely that
> we need to check the namelist-group-name for USE association in
> match.cc:gfc_match_namelist.
> 
> Hmmm, it seems we already catch the error, but accept it as an
> extension.
> 
> % gfcx -o z -std=f2023 a.f90 && ./z
> a.f90:11:23:
> 
>     11 |    namelist /nam_nml1/j
>        |                       1
> Error: GNU Extension: Namelist group name 'nam_nml1' at (1) already
> is USE associated and cannot be respecified.

ah, I overlooked this.

> 
> In hindsight, I wish GNU extensions had warnings associated with them.
> 

Should we downgrade this extension to GFC_STD_LEGACY?
Not sure when it was implemented or where it was used.

Current NAG and Intel reject it hard; Nvidia and Flang accept it.

Harald
Steve Kargl Aug. 30, 2024, 4:33 p.m. UTC | #3
On Thu, Aug 29, 2024 at 10:05:35PM +0200, Harald Anlauf wrote:
> 
> Am 29.08.24 um 21:53 schrieb Steve Kargl:
> > Thanks for the patch.  If you have not already opened a new PR for the
> > other issue with C8107, I'll open one later today.  It's likely that
> > we need to check the namelist-group-name for USE association in
> > match.cc:gfc_match_namelist.
> > 
> > Hmmm, it seems we already catch the error, but accept it as an
> > extension.
> > 
> > % gfcx -o z -std=f2023 a.f90 && ./z
> > a.f90:11:23:
> > 
> >     11 |    namelist /nam_nml1/j
> >        |                       1
> > Error: GNU Extension: Namelist group name 'nam_nml1' at (1) already
> > is USE associated and cannot be respecified.
> 
> ah, I overlooked this.
> 
> > 
> > In hindsight, I wish GNU extensions had warnings associated with them.
> > 
> 
> Should we downgrade this extension to GFC_STD_LEGACY?

I would support such a downgrade.  In fact, I would
support making -std=f2023 the default, but that might
be pushing my luck.

> Not sure when it was implemented or where it was used.

'git blame' shows the code was last touch in 2011.
I suspect that it is much older.
Harald Anlauf Aug. 30, 2024, 4:46 p.m. UTC | #4
Am 30.08.24 um 18:33 schrieb Steve Kargl:
> On Thu, Aug 29, 2024 at 10:05:35PM +0200, Harald Anlauf wrote:
>>
>> Am 29.08.24 um 21:53 schrieb Steve Kargl:
>>> Thanks for the patch.  If you have not already opened a new PR for the
>>> other issue with C8107, I'll open one later today.  It's likely that
>>> we need to check the namelist-group-name for USE association in
>>> match.cc:gfc_match_namelist.
>>>
>>> Hmmm, it seems we already catch the error, but accept it as an
>>> extension.
>>>
>>> % gfcx -o z -std=f2023 a.f90 && ./z
>>> a.f90:11:23:
>>>
>>>      11 |    namelist /nam_nml1/j
>>>         |                       1
>>> Error: GNU Extension: Namelist group name 'nam_nml1' at (1) already
>>> is USE associated and cannot be respecified.
>>
>> ah, I overlooked this.
>>
>>>
>>> In hindsight, I wish GNU extensions had warnings associated with them.
>>>
>>
>> Should we downgrade this extension to GFC_STD_LEGACY?
> 
> I would support such a downgrade.  In fact, I would
> support making -std=f2023 the default, but that might
> be pushing my luck.

Making -std=f2023 the default would likely generate testsuite
fallout.  Before considering that seriously, it would make sense to
audit the testsuite for poorly written testcases which inadvertently
use GNU extensions (these should get the appropriate dg-options
if testing the extension was desired).  That's something a
volunteer could also contribute.

>> Not sure when it was implemented or where it was used.
> 
> 'git blame' shows the code was last touch in 2011.
> I suspect that it is much older.
> 

I checked 'git log -p' output and found that the
gfc_notify_std (GFC_STD_GNU, ...) in question exists since 2005.
There was some reformatting later, last in 2011.

Harald
Steve Kargl Aug. 30, 2024, 5:03 p.m. UTC | #5
On Fri, Aug 30, 2024 at 06:46:47PM +0200, Harald Anlauf wrote:
> Am 30.08.24 um 18:33 schrieb Steve Kargl:
> > On Thu, Aug 29, 2024 at 10:05:35PM +0200, Harald Anlauf wrote:
> > > 
> > > 
> > > Should we downgrade this extension to GFC_STD_LEGACY?
> > 
> > I would support such a downgrade.  In fact, I would
> > support making -std=f2023 the default, but that might
> > be pushing my luck.
> 
> Making -std=f2023 the default would likely generate testsuite
> fallout.  Before considering that seriously, it would make sense to
> audit the testsuite for poorly written testcases which inadvertently
> use GNU extensions (these should get the appropriate dg-options
> if testing the extension was desired).  That's something a
> volunteer could also contribute.
> 

% cd gcc/fortran
% grep GFC_STD_GNU *cc | wc -l
     340

Yeah, it would likely run into some issues with a sudden change.
Perhaps, some of the list lurkers can take on the simple change
of GFC_STD_GNU to GFC_STD_LEGACY and fix the testsuite fallout.
It would be a gentle introduction to gfortran development.
diff mbox series

Patch

From 6bfeba12c86b4d0dae27d99b484f64774dd49398 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Thu, 29 Aug 2024 21:21:39 +0200
Subject: [PATCH] Fortran: fix ICE with use with rename of namelist member
 [PR116530]

gcc/fortran/ChangeLog:

	PR fortran/116530
	* trans-io.cc (transfer_namelist_element): Prevent NULL pointer
	dereference.

gcc/testsuite/ChangeLog:

	PR fortran/116530
	* gfortran.dg/use_rename_12.f90: New test.
---
 gcc/fortran/trans-io.cc                     |  3 ++-
 gcc/testsuite/gfortran.dg/use_rename_12.f90 | 27 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/use_rename_12.f90

diff --git a/gcc/fortran/trans-io.cc b/gcc/fortran/trans-io.cc
index 7ab82fa2f5b..c0baa718ef6 100644
--- a/gcc/fortran/trans-io.cc
+++ b/gcc/fortran/trans-io.cc
@@ -1692,7 +1692,8 @@  transfer_namelist_element (stmtblock_t * block, const char * var_name,
   gcc_assert (sym || c);

   /* Build the namelist object name.  */
-  if (sym && !sym->attr.use_only && sym->attr.use_rename)
+  if (sym && !sym->attr.use_only && sym->attr.use_rename
+      && sym->ns->use_stmts->rename)
     string = gfc_build_cstring_const (sym->ns->use_stmts->rename->local_name);
   else
     string = gfc_build_cstring_const (var_name);
diff --git a/gcc/testsuite/gfortran.dg/use_rename_12.f90 b/gcc/testsuite/gfortran.dg/use_rename_12.f90
new file mode 100644
index 00000000000..0447d5fe150
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/use_rename_12.f90
@@ -0,0 +1,27 @@ 
+! { dg-do compile }
+! PR fortran/116530 - ICE with member of namelist renamed by use module
+!
+! Reported by philippe.wautelet at cnrs.fr
+
+module mod_nml1
+  implicit none
+  logical :: ldiag
+  namelist /nam_nml1/ldiag
+end module mod_nml1
+
+module mod_interm
+  use mod_nml1
+end module mod_interm
+
+program ice_nml
+  use mod_nml1,        ldiag_nml1 => ldiag
+  use mod_nml1, only : ldiag_only => ldiag
+  use mod_interm
+  implicit none
+  integer :: ilu = 10
+  read(unit=ilu,nml=nam_nml1)
+  write(unit=*,nml=nam_nml1)
+  print *, ldiag
+  print *, ldiag_nml1
+  print *, ldiag_only
+end program ice_nml
--
2.35.3