public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/100954] New: writing to a character opens unit -10
@ 2021-06-08 0:12 tacodog311 at gmail dot com
2021-06-08 2:26 ` [Bug fortran/100954] " kargl at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: tacodog311 at gmail dot com @ 2021-06-08 0:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100954
Bug ID: 100954
Summary: writing to a character opens unit -10
Product: gcc
Version: 7.5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: tacodog311 at gmail dot com
Target Milestone: ---
Created attachment 50959
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50959&action=edit
save-temps output
The function "newunit" grabs unit -10 for its first choice.
Writing into a character string using "write(charstring,(I4.4)) ivalue" also
opens iounit -10.
After such a write, the "inquire" function will show the "newunit" file as
open, which means you can't check the open-ness of the original file. And
trying to close the reportedly open file unit it causes a seg fault.
String writes do not open unit -10 on gcc 7.2.0 and lower. I see it on all
versions I've checked at 7.3 and higher up to ~10.2.
The code below runs without issue on intel compilers.
$ gfortran -v
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-7
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie
--with-system-zlib --with-target-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
Sample code:
program inquire
implicit none
logical opened
character(len=256) filename
character(len=4) charstring
integer iounit, itmp, ierr
filename='testfile'
iounit=-10
inquire(iounit, opened=opened,iostat=ierr)
! not open yet
write(*,*) 'Unit',iounit,'open?',opened,ierr
! new unit grabs -10
open(newunit=iounit,file=trim(filename) )
write(*,*) 'Opened unit',iounit
! now -10 is open
inquire(iounit, opened=opened,iostat=ierr)
write(*,*) 'Unit',iounit,'open?',opened,ierr
! close it
close(iounit)
write(*,*) 'Closed unit',iounit
! now -10 is closed
inquire(iounit, opened=opened,iostat=ierr)
write(*,*) 'Unit',iounit,'open?',opened,ierr
itmp=0
write(charstring, '(I4.4)') itmp
write(*,*) 'wrote into charstring ', trim(charstring)
! now -10 is reported as open again
inquire(iounit, opened=opened,iostat=ierr)
write(*,*) 'Unit',iounit,'open?',opened,ierr
! trying to close it seg faults
if (opened) then
close(iounit)
endif
endprogram inquire
Sample output:
gfortran test.f90 -o test.x && ./test.x
Unit -10 open? F 0
Opened unit -10
Unit -10 open? T 0
Closed unit -10
Unit -10 open? F 0
wrote into charstring 0000
Unit -10 open? T 0
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
$ gfortran -v -save-temps test.f90
Driving: gfortran -v -save-temps test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr
--with-gcc-major-version-only --program-suffix=-7
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie
--with-system-zlib --with-target-system-zlib --enable-objc-gc=auto
--enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/7/f951 test.f90 -quiet -dumpbase test.f90
-mtune=generic -march=x86-64 -auxbase test -version -fintrinsic-modules-path
/usr/lib/gcc/x86_64-linux-gnu/7/finclude -o test.s
GNU Fortran (Ubuntu 7.5.0-3ubuntu1~18.04) version 7.5.0 (x86_64-linux-gnu)
compiled by GNU C version 7.5.0, GMP version 6.1.2, MPFR version 4.0.1,
MPC version 1.1.0, isl version isl-0.19-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU Fortran2008 (Ubuntu 7.5.0-3ubuntu1~18.04) version 7.5.0 (x86_64-linux-gnu)
compiled by GNU C version 7.5.0, GMP version 6.1.2, MPFR version 4.0.1,
MPC version 1.1.0, isl version isl-0.19-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
as -v --64 -o test.o test.s
GNU assembler version 2.30 (x86_64-linux-gnu) using BFD version (GNU Binutils
for Ubuntu) 2.30
Reading specs from /usr/lib/gcc/x86_64-linux-gnu/7/libgfortran.spec
rename spec lib to liborig
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/7/collect2 -plugin
/usr/lib/gcc/x86_64-linux-gnu/7/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
-plugin-opt=-fresolution=test.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lquadmath
-plugin-opt=-pass-through=-lm -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id
--eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -pie -z now -z relro
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/7/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/7
-L/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu
-L/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib -L/lib/x86_64-linux-gnu
-L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-linux-gnu/7/../../.. test.o -lgfortran -lm -lgcc_s -lgcc
-lquadmath -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-linux-gnu/7/crtendS.o
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug fortran/100954] writing to a character opens unit -10
2021-06-08 0:12 [Bug fortran/100954] New: writing to a character opens unit -10 tacodog311 at gmail dot com
@ 2021-06-08 2:26 ` kargl at gcc dot gnu.org
2021-06-08 2:48 ` tacodog311 at gmail dot com
2021-10-01 20:17 ` [Bug libfortran/100954] " anlauf at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: kargl at gcc dot gnu.org @ 2021-06-08 2:26 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100954
kargl at gcc dot gnu.org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |kargl at gcc dot gnu.org
--- Comment #1 from kargl at gcc dot gnu.org ---
(In reply to Taco Jerkface from comment #0)
> Sample code:
>
> program inquire
> implicit none
> logical opened
> character(len=256) filename
> character(len=4) charstring
>
> integer iounit, itmp, ierr
>
> filename='testfile'
>
> iounit=-10
> inquire(iounit, opened=opened,iostat=ierr)
> ! not open yet
> write(*,*) 'Unit',iounit,'open?',opened,ierr
I believe the above is invalid Fortran. Fortran 2018, pp. 216-218
R1201 io-unit is file-unit-number
R1202 file-unit-number is scalar-int-expr
A unit is either an external unit or an internal unit. An external unit
is used to refer to an external file and is specified by an asterisk or
a file-unit-number. The value of file-unit-number shall be nonnegative,
equal to one of the named constants INPUT_UNIT, OUTPUT_UNIT, or ERROR_UNIT
of the intrinsic module ISO_FORTRAN_ENV (16.10.2), the unit argument of an
active defined input/output procedure (12.6.4.8), or a NEWUNIT value
(12.5.6.12).
In the above Fortran code, iounit is negative and it is not a NEWUNIT
value. There is some wiggle room, in that
All input/output statements are permitted to refer to units that exist.
The CLOSE, INQUIRE, and WAIT statements are also permitted to refer to
units that do not exist. No other input/output statement shall refer to
a unit that does not exist.
Your code could have done 'inquire(unit=42, opened=opened,iostat=ierr)'
where 42 is nonnegative and refers to a unit that does not exist. But,
iounit=-10 is negative and does not refer to a unit opened with NEWUNIT.
For NEWUNIT, the Fortran standard requires
The variable is defined with a processor determined NEWUNIT value...
A NEWUNIT value is a negative number, and shall not be equal to -1,
any of the named constants ERROR_UNIT, INPUT_UNIT, or OUTPUT_UNIT from
the intrinsic module ISO_FORTRAN_ENV (16.10.2), any value used by the
processor for the unit argument to a defined input/output procedure,
nor any previous NEWUNIT value that identifies a file that is connected.
The unit identified by a NEWUNIT value shall not be preconnected.
So, the Fortran standard does not require -10 to be a NEWUNIT value.
>
> ! new unit grabs -10
> open(newunit=iounit,file=trim(filename) )
> write(*,*) 'Opened unit',iounit
>
> ! now -10 is open
> inquire(iounit, opened=opened,iostat=ierr)
> write(*,*) 'Unit',iounit,'open?',opened,ierr
>
> ! close it
> close(iounit)
> write(*,*) 'Closed unit',iounit
The above is conforming as it seems that gfortran has assigned -10
as the unit number via the NEWUNIT mechanism. However, after the
close statement, there is no NEWUNIT value.
> ! now -10 is closed
> inquire(iounit, opened=opened,iostat=ierr)
> write(*,*) 'Unit',iounit,'open?',opened,ierr
See above as I believe that this is invalid Fortran.
> itmp=0
> write(charstring, '(I4.4)') itmp
> write(*,*) 'wrote into charstring ', trim(charstring)
>
>
> ! now -10 is reported as open again
> inquire(iounit, opened=opened,iostat=ierr)
> write(*,*) 'Unit',iounit,'open?',opened,ierr
Invalid code can yield unexpected results.
> ! trying to close it seg faults
> if (opened) then
> close(iounit)
> endif
>
> endprogram inquire
Of course, I could be wrong, so I'll leave this for other to pondered.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug fortran/100954] writing to a character opens unit -10
2021-06-08 0:12 [Bug fortran/100954] New: writing to a character opens unit -10 tacodog311 at gmail dot com
2021-06-08 2:26 ` [Bug fortran/100954] " kargl at gcc dot gnu.org
@ 2021-06-08 2:48 ` tacodog311 at gmail dot com
2021-10-01 20:17 ` [Bug libfortran/100954] " anlauf at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: tacodog311 at gmail dot com @ 2021-06-08 2:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100954
--- Comment #2 from Taco Jerkface <tacodog311 at gmail dot com> ---
Agreed. A typical user will use unit=42, when manually picking unit numbers.
However the bug remains for automatic, newunit unit numbers.
To reproduce:
1) Open a "newunit" file.
2) close file
3) write to character string
4) "newunit" file appears open, even though it has been closed.
A more concise version of the code would be:
program inquire
implicit none
logical opened
character(len=256) filename
character(len=4) charstring
integer iounit, itmp, ierr
filename='testfile'
! new unit grabs -10
open(newunit=iounit,file=trim(filename) )
write(*,*) 'Opened unit',iounit
! now -10 is open
inquire(iounit, opened=opened,iostat=ierr)
write(*,*) 'Unit',iounit,'open?',opened,ierr
! close it
close(iounit)
write(*,*) 'Closed unit',iounit
! now -10 is closed
inquire(iounit, opened=opened,iostat=ierr)
write(*,*) 'Unit',iounit,'open?',opened,ierr
itmp=0
write(charstring, '(I4.4)') itmp
write(*,*) 'wrote into charstring ', trim(charstring)
! now -10 is reported as open again, but it is not
inquire(iounit, opened=opened,iostat=ierr)
write(*,*) 'Unit',iounit,'open?',opened,ierr
endprogram inquire
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug libfortran/100954] writing to a character opens unit -10
2021-06-08 0:12 [Bug fortran/100954] New: writing to a character opens unit -10 tacodog311 at gmail dot com
2021-06-08 2:26 ` [Bug fortran/100954] " kargl at gcc dot gnu.org
2021-06-08 2:48 ` tacodog311 at gmail dot com
@ 2021-10-01 20:17 ` anlauf at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: anlauf at gcc dot gnu.org @ 2021-10-01 20:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100954
anlauf at gcc dot gnu.org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |anlauf at gcc dot gnu.org
Status|UNCONFIRMED |NEW
Ever confirmed|0 |1
Last reconfirmed| |2021-10-01
Component|fortran |libfortran
--- Comment #3 from anlauf at gcc dot gnu.org ---
This is possibly a bookkeeping issue.
Reduced testcase:
program inquire
implicit none
integer :: iounit, ierr
logical :: opened
character(4) :: c
! open(newunit=iounit,file="testfile")
write(c,'(a)') "abcd"
iounit = -10
inquire(iounit, opened=opened,iostat=ierr)
print *,iounit,'open?',opened,ierr
iounit = -11
inquire(iounit, opened=opened,iostat=ierr)
print *,iounit,'open?',opened,ierr
endprogram inquire
This prints
-10 open? T 0
-11 open? F 0
but should print
-10 open? F 0
-11 open? F 0
Note that the start value -10 comes from unit.c:
#define NEWUNIT_START -10
If we uncomment before the internal write
open(newunit=iounit,file="testfile")
then we'll find that units -10 and -11 are opened:
-10 open? T 0
-11 open? T 0
If we swap the open and internal write, then -11 is not opened:
-10 open? T 0
-11 open? F 0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-10-01 20:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-08 0:12 [Bug fortran/100954] New: writing to a character opens unit -10 tacodog311 at gmail dot com
2021-06-08 2:26 ` [Bug fortran/100954] " kargl at gcc dot gnu.org
2021-06-08 2:48 ` tacodog311 at gmail dot com
2021-10-01 20:17 ` [Bug libfortran/100954] " anlauf at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).