public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/96486] New: get_environment_variable fails for zero-length variables
@ 2020-08-05 16:03 jussilehtola at fedoraproject dot org
  2020-08-05 16:06 ` [Bug fortran/96486] get_environment_variable fails for zero-length values kargl at gcc dot gnu.org
                   ` (32 more replies)
  0 siblings, 33 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-05 16:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

            Bug ID: 96486
           Summary: get_environment_variable fails for zero-length
                    variables
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jussilehtola at fedoraproject dot org
  Target Milestone: ---

get_environment_variable checks for zero-length value strings before it checks
the length of the result string.

In the case of zero-length result strings, a zero-length value string should be
perfectly acceptable.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
@ 2020-08-05 16:06 ` kargl at gcc dot gnu.org
  2020-08-05 16:07 ` dominiq at lps dot ens.fr
                   ` (31 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-05 16:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

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 ---
Source code?

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
  2020-08-05 16:06 ` [Bug fortran/96486] get_environment_variable fails for zero-length values kargl at gcc dot gnu.org
@ 2020-08-05 16:07 ` dominiq at lps dot ens.fr
  2020-08-05 16:07 ` dominiq at lps dot ens.fr
                   ` (30 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: dominiq at lps dot ens.fr @ 2020-08-05 16:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #2 from Dominique d'Humieres <dominiq at lps dot ens.fr> ---
Could you please provide an example?

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
  2020-08-05 16:06 ` [Bug fortran/96486] get_environment_variable fails for zero-length values kargl at gcc dot gnu.org
  2020-08-05 16:07 ` dominiq at lps dot ens.fr
@ 2020-08-05 16:07 ` dominiq at lps dot ens.fr
  2020-08-05 16:42 ` jussilehtola at fedoraproject dot org
                   ` (29 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: dominiq at lps dot ens.fr @ 2020-08-05 16:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

Dominique d'Humieres <dominiq at lps dot ens.fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2020-08-05

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (2 preceding siblings ...)
  2020-08-05 16:07 ` dominiq at lps dot ens.fr
@ 2020-08-05 16:42 ` jussilehtola at fedoraproject dot org
  2020-08-05 16:43 ` jussilehtola at fedoraproject dot org
                   ` (28 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-05 16:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #3 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
program zerolen_value
  implicit none
  character(len=*), parameter :: name='HOSTNAME'
  character(len=:), allocatable :: value
  integer :: l, err
  call get_environment_variable(name,length=l,status=err)
  if (err.ne.0) then
     stop 'System variable unassigned'
  endif
  allocate( character(len=l) :: value, stat=err )
  if (err.ne.0) then
     stop 'Variable could not be allocated'
  endif
  ! If the environment variable has not been set, l=0, and the                  
  ! following get_environment_variable call crashes.                            
!  if (l.gt.0) then                                                             
  call get_environment_variable(name,value,status=err)
  if (err.ne.0) then
     stop 'System variable corrupted'
  endif
! endif                                                                         
end program zerolen_value

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (3 preceding siblings ...)
  2020-08-05 16:42 ` jussilehtola at fedoraproject dot org
@ 2020-08-05 16:43 ` jussilehtola at fedoraproject dot org
  2020-08-05 17:36 ` kargl at gcc dot gnu.org
                   ` (27 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-05 16:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #4 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
$ export HOSTNAME=foo
$ ./a.out 
$

$ export HOSTNAME=
$ ./a.out 
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

Error termination. Backtrace:
#0  0x7f25c93aadf1 in ???
#1  0x7f25c93ab939 in ???
#2  0x7f25c93abe60 in ???
#3  0x7f25c960ce53 in ???
#4  0x401267 in ???
#5  0x4012c4 in ???
#6  0x7f25c903a041 in __libc_start_main
        at ../csu/libc-start.c:308
#7  0x4010ad in ???
#8  0xffffffffffffffff in ???
$

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (4 preceding siblings ...)
  2020-08-05 16:43 ` jussilehtola at fedoraproject dot org
@ 2020-08-05 17:36 ` kargl at gcc dot gnu.org
  2020-08-05 17:43 ` kargl at gcc dot gnu.org
                   ` (26 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-05 17:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P4
             Status|WAITING                     |NEW

--- Comment #5 from kargl at gcc dot gnu.org ---
(In reply to Susi Lehtola from comment #4)
> $ export HOSTNAME=foo
> $ ./a.out 
> $
> 
> $ export HOSTNAME=
> $ ./a.out 
> Fortran runtime error: Zero-length string passed as value to
> get_environment_variable.
> 
> Error termination. Backtrace:
> #0  0x7f25c93aadf1 in ???
> #1  0x7f25c93ab939 in ???
> #2  0x7f25c93abe60 in ???
> #3  0x7f25c960ce53 in ???
> #4  0x401267 in ???
> #5  0x4012c4 in ???
> #6  0x7f25c903a041 in __libc_start_main
> 	at ../csu/libc-start.c:308
> #7  0x4010ad in ???
> #8  0xffffffffffffffff in ???
> $

Thanks.

What gfortran version and operating system?  I get

% gfcx -o z b.f90 && ./z
STOP System variable unassigned
% gfcx -o z -g b.f90 && ./z
STOP System variable unassigned
% gfcx -o z -g -O b.f90 && ./z
STOP System variable unassigned

with your code where gfcx is gfortran 10.0.0 (Jan 2020 vintage)
and I'm using i585-*-freebsd.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (5 preceding siblings ...)
  2020-08-05 17:36 ` kargl at gcc dot gnu.org
@ 2020-08-05 17:43 ` kargl at gcc dot gnu.org
  2020-08-05 17:49 ` kargl at gcc dot gnu.org
                   ` (25 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-05 17:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #6 from kargl at gcc dot gnu.org ---
I do note there are other problems with get_environment_variable.

1) Neither length nor status can be integer(1).  gfortran should issue
   an error.

2) Fortran 2018 has added an optional errmsg argument.  This is not
   supported.

3) gfortran is issuing errors that don't make sence (to me).

program foo
   integer( 1) i01
   integer( 2) i02
   integer( 4) i04
   integer( 8) i08
#ifdef __GFC_INT_16__
   integer(16) i16
#endif
   character(len=20) name

   print '(A,I0)', 'range(i01) = ', range(i01)
   print '(A,I0)', 'range(i02) = ', range(i02)
   print '(A,I0)', 'range(i04) = ', range(i04)
   print '(A,I0)', 'range(i08) = ', range(i08)
#ifdef __GFC_INT_16__
   print '(A,I0)', 'range(i16) = ', range(i16)
#endif

   name = ''
   ! Although valid Fortran, this should probably raise a warning.
   call get_environment_variable(name);

   name = 'FOO'
   call get_environment_variable(name,length=i01);  ! Should be an error
   call get_environment_variable(name,length=i02);  ! Should be ok
   call get_environment_variable(name,length=i04);  ! Should be ok
   call get_environment_variable(name,length=i08);  ! Should be ok
#ifdef __GFC_INT_16__
   call get_environment_variable(name,length=i16);  ! Should be ok
#endif

end program foo

mobile:kargl[256] gfcx -o z a.F90 && ./z
a.F90:29:45:

   29 |    call get_environment_variable(name,length=i01);  ! Should be an
error
      |                                             1
Error: In call to 'get_environment_variable' at (1), type mismatch in argument
length'; pass 'INTEGER(1)' to 'INTEGER(4)'
a.F90:30:45:

   30 |    call get_environment_variable(name,length=i02);
      |                                             1
Error: In call to 'get_environment_variable' at (1), type mismatch in argument
length'; pass 'INTEGER(2)' to 'INTEGER(4)'
a.F90:32:45:

   32 |    call get_environment_variable(name,length=i08);
      |                                             1
Error: In call to 'get_environment_variable' at (1), type mismatch in argument
length'; pass 'INTEGER(8)' to 'INTEGER(4)'

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (6 preceding siblings ...)
  2020-08-05 17:43 ` kargl at gcc dot gnu.org
@ 2020-08-05 17:49 ` kargl at gcc dot gnu.org
  2020-08-05 17:53 ` jussilehtola at fedoraproject dot org
                   ` (24 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-05 17:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #7 from kargl at gcc dot gnu.org ---
(In reply to kargl from comment #6)
> I do note there are other problems with get_environment_variable.
> 
> 1) Neither length nor status can be integer(1).  gfortran should issue
>    an error.
> 
> 2) Fortran 2018 has added an optional errmsg argument.  This is not
>    supported.
> 
> 3) gfortran is issuing errors that don't make sence (to me).
> 
> program foo
>    integer( 1) i01
>    integer( 2) i02
>    integer( 4) i04
>    integer( 8) i08
> #ifdef __GFC_INT_16__
>    integer(16) i16
> #endif
>    character(len=20) name
> 
>    print '(A,I0)', 'range(i01) = ', range(i01)
>    print '(A,I0)', 'range(i02) = ', range(i02)
>    print '(A,I0)', 'range(i04) = ', range(i04)
>    print '(A,I0)', 'range(i08) = ', range(i08)
> #ifdef __GFC_INT_16__
>    print '(A,I0)', 'range(i16) = ', range(i16)
> #endif
> 
>    name = ''
>    ! Although valid Fortran, this should probably raise a warning.
>    call get_environment_variable(name);
> 
>    name = 'FOO'
>    call get_environment_variable(name,length=i01);  ! Should be an error
>    call get_environment_variable(name,length=i02);  ! Should be ok
>    call get_environment_variable(name,length=i04);  ! Should be ok
>    call get_environment_variable(name,length=i08);  ! Should be ok
> #ifdef __GFC_INT_16__
>    call get_environment_variable(name,length=i16);  ! Should be ok
> #endif
> 
> end program foo
> 
> mobile:kargl[256] gfcx -o z a.F90 && ./z
> a.F90:29:45:
> 
>    29 |    call get_environment_variable(name,length=i01);  ! Should be an
> error
>       |                                             1
> Error: In call to 'get_environment_variable' at (1), type mismatch in
> argument length'; pass 'INTEGER(1)' to 'INTEGER(4)'
> a.F90:30:45:
> 
>    30 |    call get_environment_variable(name,length=i02);
>       |                                             1
> Error: In call to 'get_environment_variable' at (1), type mismatch in
> argument length'; pass 'INTEGER(2)' to 'INTEGER(4)'
> a.F90:32:45:
> 
>    32 |    call get_environment_variable(name,length=i08);
>       |                                             1
> Error: In call to 'get_environment_variable' at (1), type mismatch in
> argument length'; pass 'INTEGER(8)' to 'INTEGER(4)'

Need to add a gfc_check_get_environment_variable routine to get pass these
errors.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (7 preceding siblings ...)
  2020-08-05 17:49 ` kargl at gcc dot gnu.org
@ 2020-08-05 17:53 ` jussilehtola at fedoraproject dot org
  2020-08-05 18:00 ` kargl at gcc dot gnu.org
                   ` (23 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-05 17:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #8 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
(In reply to kargl from comment #5)
> What gfortran version and operating system?  I get
> 
> % gfcx -o z b.f90 && ./z
> STOP System variable unassigned
> % gfcx -o z -g b.f90 && ./z
> STOP System variable unassigned
> % gfcx -o z -g -O b.f90 && ./z
> STOP System variable unassigned
> 
> with your code where gfcx is gfortran 10.0.0 (Jan 2020 vintage)
> and I'm using i585-*-freebsd.

This is
$ gfortran --version
GNU Fortran (GCC) 10.2.1 20200723 (Red Hat 10.2.1-1)
Copyright (C) 2020 Free Software Foundation, Inc.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (8 preceding siblings ...)
  2020-08-05 17:53 ` jussilehtola at fedoraproject dot org
@ 2020-08-05 18:00 ` kargl at gcc dot gnu.org
  2020-08-05 18:14 ` jussilehtola at fedoraproject dot org
                   ` (22 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-05 18:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #9 from kargl at gcc dot gnu.org ---
(In reply to kargl from comment #5)
> (In reply to Susi Lehtola from comment #4)
> > $ export HOSTNAME=foo
> > $ ./a.out 
> > $
> > 
> > $ export HOSTNAME=
> > $ ./a.out 
> > Fortran runtime error: Zero-length string passed as value to
> > get_environment_variable.
> > 
> > Error termination. Backtrace:
> > #0  0x7f25c93aadf1 in ???
> > #1  0x7f25c93ab939 in ???
> > #2  0x7f25c93abe60 in ???
> > #3  0x7f25c960ce53 in ???
> > #4  0x401267 in ???
> > #5  0x4012c4 in ???
> > #6  0x7f25c903a041 in __libc_start_main
> > 	at ../csu/libc-start.c:308
> > #7  0x4010ad in ???
> > #8  0xffffffffffffffff in ???
> > $
> 
> Thanks.
> 
> What gfortran version and operating system?  I get
> 
> % gfcx -o z b.f90 && ./z
> STOP System variable unassigned
> % gfcx -o z -g b.f90 && ./z
> STOP System variable unassigned
> % gfcx -o z -g -O b.f90 && ./z
> STOP System variable unassigned
> 
> with your code where gfcx is gfortran 10.0.0 (Jan 2020 vintage)
> and I'm using i585-*-freebsd.

Whoops.  Misread part of bug report. With tcsh, I see

% setenv HOSTNAME ''
% gfcx -o z -g -O b.f90 && ./z
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

and

unsetenv HOSTNAME
% setenv HOSTNAME
% gfcx -o z -g -O b.f90 && ./z
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

No backtrace.  Can you compile with -g, run the executable under gdb,
and post the backtrace.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (9 preceding siblings ...)
  2020-08-05 18:00 ` kargl at gcc dot gnu.org
@ 2020-08-05 18:14 ` jussilehtola at fedoraproject dot org
  2020-08-05 18:33 ` kargl at gcc dot gnu.org
                   ` (21 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-05 18:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #10 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
Compiled with -g, rerun gives

$ ./a.out 
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

Error termination. Backtrace:
#0  0x401267 in zerolen_value
        at /tmp/readvar2.f90:17
#1  0x4012c4 in main
        at /tmp/readvar2.f90:22

This is the second call.

The problem originates in
https://gcc.gnu.org/git/?p=gcc.git;a=blob_plain;f=libgfortran/intrinsics/env.c;hb=HEAD
where the size checks are in an incorrect order.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (10 preceding siblings ...)
  2020-08-05 18:14 ` jussilehtola at fedoraproject dot org
@ 2020-08-05 18:33 ` kargl at gcc dot gnu.org
  2020-08-06  8:03 ` markeggleston at gcc dot gnu.org
                   ` (20 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-05 18:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #11 from kargl at gcc dot gnu.org ---
(In reply to Susi Lehtola from comment #10)
> Compiled with -g, rerun gives
> 
> $ ./a.out 
> Fortran runtime error: Zero-length string passed as value to
> get_environment_variable.
> 
> Error termination. Backtrace:
> #0  0x401267 in zerolen_value
> 	at /tmp/readvar2.f90:17
> #1  0x4012c4 in main
> 	at /tmp/readvar2.f90:22
> 
> This is the second call.
> 
> The problem originates in
> https://gcc.gnu.org/git/?p=gcc.git;a=blob_plain;f=libgfortran/intrinsics/env.
> c;hb=HEAD where the size checks are in an incorrect order.

Sorry, I don't understand what you mean by incorrect order.

Can you please run a.out under gdb and post the backtrace

% gdb ./a.out
(gdb) run

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (11 preceding siblings ...)
  2020-08-05 18:33 ` kargl at gcc dot gnu.org
@ 2020-08-06  8:03 ` markeggleston at gcc dot gnu.org
  2020-08-06  8:42 ` jussilehtola at fedoraproject dot org
                   ` (19 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: markeggleston at gcc dot gnu.org @ 2020-08-06  8:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

markeggleston at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |markeggleston at gcc dot gnu.org

--- Comment #12 from markeggleston at gcc dot gnu.org ---
(In reply to kargl from comment #6)
> I do note there are other problems with get_environment_variable.

It looks to me that this intrinsic supports the Fortran 2003 standard. Changes
were introduced with the Fortran 2018 and support has not yet been implemented.

> 
> 1) Neither length nor status can be integer(1).  gfortran should issue
>    an error.

Fortran 2003 has default kind for these arguments.

> 
> 2) Fortran 2018 has added an optional errmsg argument.  This is not
>    supported.
> 
> 3) gfortran is issuing errors that don't make sence (to me).
> 
> program foo
>    integer( 1) i01
>    integer( 2) i02
>    integer( 4) i04
>    integer( 8) i08
> #ifdef __GFC_INT_16__
>    integer(16) i16
> #endif
>    character(len=20) name
> 
>    print '(A,I0)', 'range(i01) = ', range(i01)
>    print '(A,I0)', 'range(i02) = ', range(i02)
>    print '(A,I0)', 'range(i04) = ', range(i04)
>    print '(A,I0)', 'range(i08) = ', range(i08)
> #ifdef __GFC_INT_16__
>    print '(A,I0)', 'range(i16) = ', range(i16)
> #endif
> 
>    name = ''
>    ! Although valid Fortran, this should probably raise a warning.
>    call get_environment_variable(name);
> 
>    name = 'FOO'
>    call get_environment_variable(name,length=i01);  ! Should be an error
>    call get_environment_variable(name,length=i02);  ! Should be ok
>    call get_environment_variable(name,length=i04);  ! Should be ok
>    call get_environment_variable(name,length=i08);  ! Should be ok
> #ifdef __GFC_INT_16__
>    call get_environment_variable(name,length=i16);  ! Should be ok
> #endif
> 
> end program foo
> 
> mobile:kargl[256] gfcx -o z a.F90 && ./z
> a.F90:29:45:
> 
>    29 |    call get_environment_variable(name,length=i01);  ! Should be an
> error
>       |                                             1
> Error: In call to 'get_environment_variable' at (1), type mismatch in
> argument length'; pass 'INTEGER(1)' to 'INTEGER(4)'
> a.F90:30:45:
> 
>    30 |    call get_environment_variable(name,length=i02);
>       |                                             1
> Error: In call to 'get_environment_variable' at (1), type mismatch in
> argument length'; pass 'INTEGER(2)' to 'INTEGER(4)'
> a.F90:32:45:
> 
>    32 |    call get_environment_variable(name,length=i08);
>       |                                             1
> Error: In call to 'get_environment_variable' at (1), type mismatch in
> argument length'; pass 'INTEGER(8)' to 'INTEGER(4)'

The mismatch errors are consistent with Fortran 2003 support.

I think these issues should be in a separate PR for Fortran 2018 support.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (12 preceding siblings ...)
  2020-08-06  8:03 ` markeggleston at gcc dot gnu.org
@ 2020-08-06  8:42 ` jussilehtola at fedoraproject dot org
  2020-08-06 15:56 ` sgk at troutmask dot apl.washington.edu
                   ` (18 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-06  8:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #13 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
The gdb output is the same

(gdb) r
Starting program: /tmp/a.out 
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

Error termination. Backtrace:
#0  0x401267 in zerolen_value
        at /tmp/readvar2.f90:17
#1  0x4012c4 in main
        at /tmp/readvar2.f90:22
[Inferior 1 (process 122282) exited with code 02]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (13 preceding siblings ...)
  2020-08-06  8:42 ` jussilehtola at fedoraproject dot org
@ 2020-08-06 15:56 ` sgk at troutmask dot apl.washington.edu
  2020-08-06 16:01 ` sgk at troutmask dot apl.washington.edu
                   ` (17 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2020-08-06 15:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #14 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Thu, Aug 06, 2020 at 08:03:29AM +0000, markeggleston at gcc dot gnu.org
wrote:
> (In reply to kargl from comment #6)
> > I do note there are other problems with get_environment_variable.
> 
> It looks to me that this intrinsic supports the Fortran 2003
> standard. Changes were introduced with the Fortran 2018 and
> support has not yet been implemented.

Yep

> > 
> > 1) Neither length nor status can be integer(1).  gfortran should issue
> >    an error.
> 
> Fortran 2003 has default kind for these arguments.
> 

As does F2008.

> > 
> > 2) Fortran 2018 has added an optional errmsg argument.  This is not
> >    supported.
> > 
> > 3) gfortran is issuing errors that don't make sence (to me).
> > 
> > program foo
> >    integer( 1) i01
> >    integer( 2) i02
> >    integer( 4) i04
> >    integer( 8) i08
> > #ifdef __GFC_INT_16__
> >    integer(16) i16
> > #endif
> >    character(len=20) name
> > 
> >    print '(A,I0)', 'range(i01) = ', range(i01)
> >    print '(A,I0)', 'range(i02) = ', range(i02)
> >    print '(A,I0)', 'range(i04) = ', range(i04)
> >    print '(A,I0)', 'range(i08) = ', range(i08)
> > #ifdef __GFC_INT_16__
> >    print '(A,I0)', 'range(i16) = ', range(i16)
> > #endif
> > 
> >    name = ''
> >    ! Although valid Fortran, this should probably raise a warning.
> >    call get_environment_variable(name);
> > 
> >    name = 'FOO'
> >    call get_environment_variable(name,length=i01);  ! Should be an error
> >    call get_environment_variable(name,length=i02);  ! Should be ok
> >    call get_environment_variable(name,length=i04);  ! Should be ok
> >    call get_environment_variable(name,length=i08);  ! Should be ok
> > #ifdef __GFC_INT_16__
> >    call get_environment_variable(name,length=i16);  ! Should be ok
> > #endif
> > 
> > end program foo
> > 
> > mobile:kargl[256] gfcx -o z a.F90 && ./z
> > a.F90:29:45:
> > 
> >    29 |    call get_environment_variable(name,length=i01);  ! Should be an
> > error
> >       |                                             1
> > Error: In call to 'get_environment_variable' at (1), type mismatch in
> > argument length'; pass 'INTEGER(1)' to 'INTEGER(4)'
> > a.F90:30:45:
> > 
> >    30 |    call get_environment_variable(name,length=i02);
> >       |                                             1
> > Error: In call to 'get_environment_variable' at (1), type mismatch in
> > argument length'; pass 'INTEGER(2)' to 'INTEGER(4)'
> > a.F90:32:45:
> > 
> >    32 |    call get_environment_variable(name,length=i08);
> >       |                                             1
> > Error: In call to 'get_environment_variable' at (1), type mismatch in
> > argument length'; pass 'INTEGER(8)' to 'INTEGER(4)'
> 
> The mismatch errors are consistent with Fortran 2003 support.
> 
> I think these issues should be in a separate PR for Fortran 2018 support.
> 

gfortran is using the default argument checking routine, which
is based on the information from intrinsic.c.  With f2018,
making (almost?) everything generic, a gfc_check_environment_variable
is needed.  This then may allow some of the runtime checking
to be removed.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (14 preceding siblings ...)
  2020-08-06 15:56 ` sgk at troutmask dot apl.washington.edu
@ 2020-08-06 16:01 ` sgk at troutmask dot apl.washington.edu
  2020-08-06 20:33 ` jussilehtola at fedoraproject dot org
                   ` (16 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2020-08-06 16:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #15 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Thu, Aug 06, 2020 at 08:42:20AM +0000, jussilehtola at fedoraproject dot org
wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486
> 
> --- Comment #13 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
> The gdb output is the same
> 
> (gdb) r
> Starting program: /tmp/a.out 
> Fortran runtime error: Zero-length string passed as value to
> get_environment_variable.
> 
> Error termination. Backtrace:
> #0  0x401267 in zerolen_value
>         at /tmp/readvar2.f90:17
> #1  0x4012c4 in main
>         at /tmp/readvar2.f90:22
> [Inferior 1 (process 122282) exited with code 02]
> 

I cannot reproduce your error.  Your original backtrace was

Error termination. Backtrace:
#0  0x7f25c93aadf1 in ???
#1  0x7f25c93ab939 in ???
#2  0x7f25c93abe60 in ???
#3  0x7f25c960ce53 in ???
#4  0x401267 in ???
#5  0x4012c4 in ???
#6  0x7f25c903a041 in __libc_start_main
 at ../csu/libc-start.c:308
#7  0x4010ad in ???
#8  0xffffffffffffffff in ???

I would expect a long backtrace under gdb.  Don't know
what [Inferior 1...] means.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (15 preceding siblings ...)
  2020-08-06 16:01 ` sgk at troutmask dot apl.washington.edu
@ 2020-08-06 20:33 ` jussilehtola at fedoraproject dot org
  2020-08-07  0:17 ` sgk at troutmask dot apl.washington.edu
                   ` (15 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-06 20:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #16 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
Yes, then I installed missing libgfortran debuginfos and the backtrace
shortened to

#0  0x401267 in zerolen_value
        at /tmp/readvar2.f90:17
#1  0x4012c4 in main
        at /tmp/readvar2.f90:22

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (16 preceding siblings ...)
  2020-08-06 20:33 ` jussilehtola at fedoraproject dot org
@ 2020-08-07  0:17 ` sgk at troutmask dot apl.washington.edu
  2020-08-07  3:02 ` kargl at gcc dot gnu.org
                   ` (14 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2020-08-07  0:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #17 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Thu, Aug 06, 2020 at 08:33:12PM +0000, jussilehtola at fedoraproject dot org
wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486
> 
> --- Comment #16 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
> Yes, then I installed missing libgfortran debuginfos and the backtrace
> shortened to
> 
> #0  0x401267 in zerolen_value
>         at /tmp/readvar2.f90:17
> #1  0x4012c4 in main
>         at /tmp/readvar2.f90:22
> 

I cannot reproduce the issue.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (17 preceding siblings ...)
  2020-08-07  0:17 ` sgk at troutmask dot apl.washington.edu
@ 2020-08-07  3:02 ` kargl at gcc dot gnu.org
  2020-08-07 13:14 ` jussilehtola at fedoraproject dot org
                   ` (13 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: kargl at gcc dot gnu.org @ 2020-08-07  3:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #18 from kargl at gcc dot gnu.org ---
Created attachment 49015
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49015&action=edit
Bring get_environment_variable almost to F2018 conformance

* check.c (gfc_check_get_environment_variable):  New function.  Implement
  checking for get_environment_variable.

* gfortran.h: Add member for 6-argument subroutine to gfc_check_f.

* intrinsic.c (do_check): Deal with 6-argument subroutine checking.
  (add_sym_6s): Add 6-argument subroutine to subroutine list.
  (add_subroutines): Use add_sym_6s to add get_envronment_variable.

* intrinsic.h: Prototype for gfc_check_get_environment_variable.

* libgfortran/intrinsics/env.c:
  . Add F2018 errmsg variable.
  . Remove runtime error for missing name variable.  This is caught in
    check.c.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (18 preceding siblings ...)
  2020-08-07  3:02 ` kargl at gcc dot gnu.org
@ 2020-08-07 13:14 ` jussilehtola at fedoraproject dot org
  2020-08-07 15:34 ` sgk at troutmask dot apl.washington.edu
                   ` (12 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-07 13:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #19 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
(In reply to Steve Kargl from comment #17)
> On Thu, Aug 06, 2020 at 08:33:12PM +0000, jussilehtola at fedoraproject dot
> org wrote:
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486
> > 
> > --- Comment #16 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
> > Yes, then I installed missing libgfortran debuginfos and the backtrace
> > shortened to
> > 
> > #0  0x401267 in zerolen_value
> >         at /tmp/readvar2.f90:17
> > #1  0x4012c4 in main
> >         at /tmp/readvar2.f90:22
> > 
> 
> I cannot reproduce the issue.

It already was reproduced in comment #9?

The issue is not that the environment variable has not been set, but rather
that it is set to an empty string.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (19 preceding siblings ...)
  2020-08-07 13:14 ` jussilehtola at fedoraproject dot org
@ 2020-08-07 15:34 ` sgk at troutmask dot apl.washington.edu
  2020-08-07 20:35 ` jussilehtola at fedoraproject dot org
                   ` (11 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2020-08-07 15:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #20 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Fri, Aug 07, 2020 at 01:14:04PM +0000, jussilehtola at fedoraproject dot org
wrote:
> 
> It already was reproduced in comment #9?
> 

There is clearly a language barrier.  At this point, I have
no idea what you think the issue is, and I don't care.  Good
luck getting anyone else to look this bug report.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable fails for zero-length values
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (20 preceding siblings ...)
  2020-08-07 15:34 ` sgk at troutmask dot apl.washington.edu
@ 2020-08-07 20:35 ` jussilehtola at fedoraproject dot org
  2020-08-07 20:36 ` [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings jussilehtola at fedoraproject dot org
                   ` (10 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-07 20:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #21 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
to repeat: libgfortran crashes when you try to read an environment variable
that is an empty string, in the case you have preallocated an array that is the
proper size i.e. zero characters.

I don't get any more backtrace than what I've reported above; I guess gdb
doesn't debug libgfortran intrinsics by default. The procedure I used was

$ gfortran -g -Wall readvar2.f90 
$ export HOSTNAME=
$ gdb ./a.out
(gdb) r
Starting program: /tmp/a.out 
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

Error termination. Backtrace:
#0  0x401267 in zerolen_value
        at /tmp/readvar2.f90:17
#1  0x4012c4 in main
        at /tmp/readvar2.f90:22
[Inferior 1 (process 152269) exited with code 02]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (21 preceding siblings ...)
  2020-08-07 20:35 ` jussilehtola at fedoraproject dot org
@ 2020-08-07 20:36 ` jussilehtola at fedoraproject dot org
  2020-08-07 20:42 ` jussilehtola at fedoraproject dot org
                   ` (9 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-07 20:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

Susi Lehtola <jussilehtola at fedoraproject dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|get_environment_variable    |get_environment_variable
                   |fails for zero-length       |crashes for environment
                   |values                      |variables that are empty
                   |                            |strings

--- Comment #22 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
to repeat: libgfortran crashes when you try to read an environment variable
that is an empty string, in the case you have preallocated an array that is the
proper size i.e. zero characters.

I don't get any more backtrace than what I've reported above; I guess gdb
doesn't debug libgfortran intrinsics by default. The procedure I used was

$ gfortran -g -Wall readvar2.f90 
$ export HOSTNAME=
$ gdb ./a.out
(gdb) r
Starting program: /tmp/a.out 
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

Error termination. Backtrace:
#0  0x401267 in zerolen_value
        at /tmp/readvar2.f90:17
#1  0x4012c4 in main
        at /tmp/readvar2.f90:22
[Inferior 1 (process 152269) exited with code 02]

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (22 preceding siblings ...)
  2020-08-07 20:36 ` [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings jussilehtola at fedoraproject dot org
@ 2020-08-07 20:42 ` jussilehtola at fedoraproject dot org
  2020-08-07 21:19 ` sgk at troutmask dot apl.washington.edu
                   ` (8 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-07 20:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #23 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
The expected result is that the program does not crash, since a zero-size
character array is enough to fit an empty string.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (23 preceding siblings ...)
  2020-08-07 20:42 ` jussilehtola at fedoraproject dot org
@ 2020-08-07 21:19 ` sgk at troutmask dot apl.washington.edu
  2020-08-07 21:55 ` schwab@linux-m68k.org
                   ` (7 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2020-08-07 21:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #24 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Fri, Aug 07, 2020 at 08:35:49PM +0000, jussilehtola at fedoraproject dot org
wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486
> 
> --- Comment #21 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
> to repeat: libgfortran crashes when you try to read an environment variable
> that is an empty string, in the case you have preallocated an array that is the
> proper size i.e. zero characters.

to repeat: I cannot reproduce your issue.  It does not crash.
It exits with a runtime error.

% setenv HOSTNAME
% gfcx -o z b.f90
% ./z
Fortran runtime error: Zero-length string passed as value to
get_environment_variable.

That's it.  No crash.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (24 preceding siblings ...)
  2020-08-07 21:19 ` sgk at troutmask dot apl.washington.edu
@ 2020-08-07 21:55 ` schwab@linux-m68k.org
  2020-08-07 22:13 ` sgk at troutmask dot apl.washington.edu
                   ` (6 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: schwab@linux-m68k.org @ 2020-08-07 21:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #25 from Andreas Schwab <schwab@linux-m68k.org> ---
But why does it error out?  It shouldn't!

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (25 preceding siblings ...)
  2020-08-07 21:55 ` schwab@linux-m68k.org
@ 2020-08-07 22:13 ` sgk at troutmask dot apl.washington.edu
  2020-08-07 22:22 ` harald at gigawatt dot nl
                   ` (5 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2020-08-07 22:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #26 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Fri, Aug 07, 2020 at 09:55:06PM +0000, schwab@linux-m68k.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486
> 
> --- Comment #25 from Andreas Schwab <schwab@linux-m68k.org> ---
> But why does it error out?  It shouldn't!
> 

You'll have to ask Paul Brook why he chose to issue an error.

% svn annotate libgfortran/intrinsic/env.c
...
 85656     pbrook   if (value != NULL)
 85656     pbrook     { 
 85656     pbrook       if (value_len < 1)
 85656     pbrook       runtime_error ("Zero-length string passed as value to "
 85656     pbrook                      "get_environment_variable.");
 85656     pbrook       else
 85656     pbrook       memset (value, ' ', value_len); /* Blank the string. 
*/
 85656     pbrook     }

IIRC, r85656 is tree-ssa becoming pre-gcc 4.0.0.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (26 preceding siblings ...)
  2020-08-07 22:13 ` sgk at troutmask dot apl.washington.edu
@ 2020-08-07 22:22 ` harald at gigawatt dot nl
  2020-08-08 15:49 ` jussilehtola at fedoraproject dot org
                   ` (4 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: harald at gigawatt dot nl @ 2020-08-07 22:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

Harald van Dijk <harald at gigawatt dot nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |harald at gigawatt dot nl

--- Comment #27 from Harald van Dijk <harald at gigawatt dot nl> ---
(In reply to Steve Kargl from comment #24)
> On Fri, Aug 07, 2020 at 08:35:49PM +0000, jussilehtola at fedoraproject dot
> org wrote:
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486
> > 
> > --- Comment #21 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
> > to repeat: libgfortran crashes when you try to read an environment variable
> > that is an empty string, in the case you have preallocated an array that is the
> > proper size i.e. zero characters.
> 
> to repeat: I cannot reproduce your issue.  It does not crash.
> It exits with a runtime error.

It seems pretty clear to me that that runtime error is what "crash" refers to.
If you are merely wondering why a runtime error does not result in a backtrace
being printed for you: the default GCC 6+ behaviour is that runtime errors
cause a backtrace to be printed, but this can be changed by
-fbacktrace/-fno-backtrace compiler options and the GFORTRAN_ERROR_BACKTRACE
runtime environment variable. That is irrelevant to this bug though: the fact
that this program results in a runtime error is what this bug is about, not
which details get printed as a result of that runtime error. Your output shows
that you did manage to reproduce the issue just fine.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (27 preceding siblings ...)
  2020-08-07 22:22 ` harald at gigawatt dot nl
@ 2020-08-08 15:49 ` jussilehtola at fedoraproject dot org
  2020-08-24 10:13 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: jussilehtola at fedoraproject dot org @ 2020-08-08 15:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #28 from Susi Lehtola <jussilehtola at fedoraproject dot org> ---
Harald, Andreas, thanks for clarifying: yes, "crash" == Fortran runtime error.
The program should run just fine, since the return array has the proper size
given by get_environment_variable. Instead, it ends in a runtime error in
get_environment_variable.

The root cause is that get_environment_variable_i4 in
libgfortran/intrinsics/env.c checks for a zero size return array before it
checks what size an array is needed: when the environment variable is set to an
empty string, a zero-size Fortran array is sufficient. The return value size
check should just be moved a few lines down to where the memcpy() is performed.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (28 preceding siblings ...)
  2020-08-08 15:49 ` jussilehtola at fedoraproject dot org
@ 2020-08-24 10:13 ` cvs-commit at gcc dot gnu.org
  2020-08-24 11:36 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  32 siblings, 0 replies; 34+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-08-24 10:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #29 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Mark Eggleston
<markeggleston@gcc.gnu.org>:

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

commit r11-2819-gde09e7ebc9d5555653745a103eef2b20c7f1dd76
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Mon Aug 10 08:07:39 2020 +0100

    Fortran  :  get_environment_variable runtime error PR96486

    Runtime error occurs when the type of the value argument is
    character(0):  "Zero-length string passed as value...".
    The status argument, intent(out), will contain -1 if the value
    of the environment is too large to fit in the value argument, this
    is the case if the type is character(0) so there is no reason to
    produce a runtime error if the value argument is zero length.

    2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

    libgfortran/

            PR fortran/96486
            * intrinsics/env.c: If value_len is > 0 blank the string.
            Copy the result only if its length is > 0.

    2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

    gcc/testsuite/

            PR fortran/96486
            * gfortran.dg/pr96486.f90: New test.

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (29 preceding siblings ...)
  2020-08-24 10:13 ` cvs-commit at gcc dot gnu.org
@ 2020-08-24 11:36 ` cvs-commit at gcc dot gnu.org
  2020-08-24 13:51 ` cvs-commit at gcc dot gnu.org
  2020-08-24 14:05 ` markeggleston at gcc dot gnu.org
  32 siblings, 0 replies; 34+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-08-24 11:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #30 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Mark Eggleston
<markeggleston@gcc.gnu.org>:

https://gcc.gnu.org/g:5effbd0733f9a4d42ddae965e4c28701be7811ac

commit r10-8658-g5effbd0733f9a4d42ddae965e4c28701be7811ac
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Mon Aug 10 08:07:39 2020 +0100

    Fortran  :  get_environment_variable runtime error PR96486

    Runtime error occurs when the type of the value argument is
    character(0):  "Zero-length string passed as value...".
    The status argument, intent(out), will contain -1 if the value
    of the environment is too large to fit in the value argument, this
    is the case if the type is character(0) so there is no reason to
    produce a runtime error if the value argument is zero length.

    2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

    libgfortran/

            PR fortran/96486
            * intrinsics/env.c: If value_len is > 0 blank the string.
            Copy the result only if its length is > 0.

    2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

    gcc/testsuite/

            PR fortran/96486
            * gfortran.dg/pr96486.f90: New test.

    (cherry picked from commit de09e7ebc9d5555653745a103eef2b20c7f1dd76)

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (30 preceding siblings ...)
  2020-08-24 11:36 ` cvs-commit at gcc dot gnu.org
@ 2020-08-24 13:51 ` cvs-commit at gcc dot gnu.org
  2020-08-24 14:05 ` markeggleston at gcc dot gnu.org
  32 siblings, 0 replies; 34+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-08-24 13:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

--- Comment #31 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Mark Eggleston
<markeggleston@gcc.gnu.org>:

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

commit r9-8829-gbbe17767c602f1ff08a1520a1d989c6b86b536fd
Author: Mark Eggleston <markeggleston@gcc.gnu.org>
Date:   Mon Aug 10 08:07:39 2020 +0100

    Fortran  :  get_environment_variable runtime error PR96486

    Runtime error occurs when the type of the value argument is
    character(0):  "Zero-length string passed as value...".
    The status argument, intent(out), will contain -1 if the value
    of the environment is too large to fit in the value argument, this
    is the case if the type is character(0) so there is no reason to
    produce a runtime error if the value argument is zero length.

    2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

    libgfortran/

            PR fortran/96486
            * intrinsics/env.c: If value_len is > 0 blank the string.
            Copy the result only if its length is > 0.

    2020-08-24  Mark Eggleston  <markeggleston@gcc.gnu.org>

    gcc/testsuite/

            PR fortran/96486
            * gfortran.dg/pr96486.f90: New test.

    (cherry picked from commit de09e7ebc9d5555653745a103eef2b20c7f1dd76)

^ permalink raw reply	[flat|nested] 34+ messages in thread

* [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings
  2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
                   ` (31 preceding siblings ...)
  2020-08-24 13:51 ` cvs-commit at gcc dot gnu.org
@ 2020-08-24 14:05 ` markeggleston at gcc dot gnu.org
  32 siblings, 0 replies; 34+ messages in thread
From: markeggleston at gcc dot gnu.org @ 2020-08-24 14:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96486

markeggleston at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #32 from markeggleston at gcc dot gnu.org ---
Committed to master and backported to gcc-10 and gcc-9.

For F2018 conformance see PR96583.

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2020-08-24 14:05 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-05 16:03 [Bug fortran/96486] New: get_environment_variable fails for zero-length variables jussilehtola at fedoraproject dot org
2020-08-05 16:06 ` [Bug fortran/96486] get_environment_variable fails for zero-length values kargl at gcc dot gnu.org
2020-08-05 16:07 ` dominiq at lps dot ens.fr
2020-08-05 16:07 ` dominiq at lps dot ens.fr
2020-08-05 16:42 ` jussilehtola at fedoraproject dot org
2020-08-05 16:43 ` jussilehtola at fedoraproject dot org
2020-08-05 17:36 ` kargl at gcc dot gnu.org
2020-08-05 17:43 ` kargl at gcc dot gnu.org
2020-08-05 17:49 ` kargl at gcc dot gnu.org
2020-08-05 17:53 ` jussilehtola at fedoraproject dot org
2020-08-05 18:00 ` kargl at gcc dot gnu.org
2020-08-05 18:14 ` jussilehtola at fedoraproject dot org
2020-08-05 18:33 ` kargl at gcc dot gnu.org
2020-08-06  8:03 ` markeggleston at gcc dot gnu.org
2020-08-06  8:42 ` jussilehtola at fedoraproject dot org
2020-08-06 15:56 ` sgk at troutmask dot apl.washington.edu
2020-08-06 16:01 ` sgk at troutmask dot apl.washington.edu
2020-08-06 20:33 ` jussilehtola at fedoraproject dot org
2020-08-07  0:17 ` sgk at troutmask dot apl.washington.edu
2020-08-07  3:02 ` kargl at gcc dot gnu.org
2020-08-07 13:14 ` jussilehtola at fedoraproject dot org
2020-08-07 15:34 ` sgk at troutmask dot apl.washington.edu
2020-08-07 20:35 ` jussilehtola at fedoraproject dot org
2020-08-07 20:36 ` [Bug fortran/96486] get_environment_variable crashes for environment variables that are empty strings jussilehtola at fedoraproject dot org
2020-08-07 20:42 ` jussilehtola at fedoraproject dot org
2020-08-07 21:19 ` sgk at troutmask dot apl.washington.edu
2020-08-07 21:55 ` schwab@linux-m68k.org
2020-08-07 22:13 ` sgk at troutmask dot apl.washington.edu
2020-08-07 22:22 ` harald at gigawatt dot nl
2020-08-08 15:49 ` jussilehtola at fedoraproject dot org
2020-08-24 10:13 ` cvs-commit at gcc dot gnu.org
2020-08-24 11:36 ` cvs-commit at gcc dot gnu.org
2020-08-24 13:51 ` cvs-commit at gcc dot gnu.org
2020-08-24 14:05 ` markeggleston 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).