【问题标题】:Truncation of deferred-length string when passing as optional作为可选传递时截断延迟长度的字符串
【发布时间】: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 上发布。但我想先在这里得到一些反馈,特别是:

  1. 我的测试用例是否符合标准?我对deepest_call 的str=str 参数特别好奇。我经常使用这种结构将参数作为可选传递,当且仅当它在当前范围内是可选的,但我无法在标准中轻松找到它,我不确定它是否真的有效。不过,仅传递 str 似乎会产生相同的行为。

  2. 有没有简单的解决方法?鉴于此问题会影响广泛的版本(4.8.5 到 8.2.0),简单地避免它们是不可行的。

  3. 有人知道其他版本的 gfortran 或其他编译器的这种行为吗?我只能轻松访问 GCC、Intel 和 PGI。

【问题讨论】:

标签: fortran gfortran


【解决方案1】:

您的代码符合标准。

一个可选的虚拟参数可能是后续调用中没有关键字的实际虚拟参数,无论它是否存在。 (当然,它可能仅在以下虚拟参数是可选的情况下不存在。)即:

call  interface_call(str)

和

一样正确
call  interface_call(str=str)

至于(次优)解决方法(我发现使用 gfortran 9.1.0 会失败,但使用 gfortran 10.0.0 20190625),您可以考虑将参数设置为非延迟长度。毕竟,您到处都在使用trim,所以尾随空格会被占用。


您的代码与不合规只差一个小改动:写语句不受可选虚拟对象的存在检查保护。如果调用链中不存在可选的虚拟对象,则程序将无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2014-08-05
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2018-03-01
    • 2015-07-08
    • 2017-06-13
    相关资源
    最近更新 更多