【发布时间】:2020-05-30 11:00:13
【问题描述】:
我在子例程之间传递可选的延迟长度字符串 (character(len=:), allocatable, optional) 并在 GCC 中出现意外行为。
这是我的最小示例,我通过接口例程将可选字符串传递给设置它的例程:
$ cat main.f90
module deepest_call_m
implicit none
contains
subroutine deepest_call(str)
character(len=:), allocatable, intent(OUT), optional :: str
if (present(str)) str = '12345'
write(*,*) 'at bot of deepest_call, str is "'//trim(str)//'"'
end subroutine deepest_call
end module deepest_call_m
module interface_call_m
implicit none
contains
subroutine interface_call(str)
use deepest_call_m, only : deepest_call
character(len=:), allocatable, intent(OUT), optional :: str
call deepest_call(str=str)
write(*,*) 'at bot of interface_call, str is "'//trim(str)//'"'
end subroutine interface_call
end module interface_call_m
program main
use interface_call_m, only : interface_call
implicit none
character(len=:), allocatable :: str
call interface_call(str=str)
write(*,*) 'at bot of main, str is "'//trim(str)//'"'
end program main
(请注意,为简单起见,我没有将 write 语句包装在 if(present) 和 if(allocated) 中,尽管这对于实际实现是必要的。)
在 Intel 16.0 和 2019_U4 以及 PGI 15.10 中,此例程给出了预期的结果:str 在 deepest_call 中设置,并且在 interface_call 和 main 中保持不变:
$ ifort --version
ifort (IFORT) 16.0.0 20150815
Copyright (C) 1985-2015 Intel Corporation. All rights reserved.
$ ifort main.f90 && ./a.out
at bot of deepest_call, str is "12345"
at bot of interface_call, str is "12345"
at bot of main, str is "12345"
但是,对于 gfortran 4.8.5:
$ gfortran --version
GNU Fortran (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING
$ gfortran main.f90 && ./a.out
at bot of deepest_call, str is "12345"
at bot of interface_call, str is ""
at bot of main, str is ""
从interface_call 中的deepest_call 返回时,字符串已被截断。在 gfortran 7.3.0 和 8.2.0 中,如果没有提供编译时选项,代码会在运行时崩溃:
[chaud106@epyc-login-1-0 Testing]$ gfortran --version && gfortran main.f90 && ./a.out
GNU Fortran (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
at bot of deepest_call, str is "12345"
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x2af39a82733f in ???
#1 0x400c68 in ???
#2 0x400d9c in ???
#3 0x400f0e in ???
#4 0x2af39a813494 in ???
#5 0x400878 in ???
#6 0xffffffffffffffff in ???
Segmentation fault
但是,添加编译时检查可以恢复之前的截断行为:
$ gfortran --version
GNU Fortran (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gfortran -g -fbacktrace -Wall -Wextra -std=f2008 -fcheck=all -Og main.f90 && ./a.out
at bot of deepest_call, str is "12345"
at bot of interface_call, str is ""
at bot of main, str is ""
在我看来,这很像一个编译器错误,所以也许我只需要在 bugzilla 上发布。但我想先在这里得到一些反馈,特别是:
我的测试用例是否符合标准?我对
deepest_call的str=str参数特别好奇。我经常使用这种结构将参数作为可选传递,当且仅当它在当前范围内是可选的,但我无法在标准中轻松找到它,我不确定它是否真的有效。不过,仅传递str似乎会产生相同的行为。有没有简单的解决方法?鉴于此问题会影响广泛的版本(4.8.5 到 8.2.0),简单地避免它们是不可行的。
有人知道其他版本的 gfortran 或其他编译器的这种行为吗?我只能轻松访问 GCC、Intel 和 PGI。
【问题讨论】:
-
如果有人好奇,这是在 bugzilla 上发布的,确实是一个错误,可能在所有支持该功能的版本中:gcc.gnu.org/bugzilla/show_bug.cgi?id=93762