Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29249 Discussions

ifx segmentation fault with elemental functions

DataScientist
Valued Contributor I
122 Views

The following code is valid and generates the correct result with ifort and gfortran, but yields a segmentation fault with ifx 2025.2 and most likely the older versions (it has taken me a year to isolate and reproduce this bug from a multimillion-line codebase):

module str_mod

    type :: str_type
        character(:), allocatable :: val
    end type

    interface str_type
        module procedure :: str_maker
    end interface

contains

    pure elemental function endswith(str, suffix) result(itis)
        type(str_type), intent(in) :: str, suffix
        logical :: itis
        itis = str%val(max(1, len(str%val) - len(suffix%val) + 1) : len(str%val)) == suffix%val
    end function

    pure elemental function str_maker(val) result(str)
        character(*), intent(in) :: val
        type(str_type) :: str
        str%val = trim(val)
    end function

end module str_mod

use str_mod, only: str_type, endswith

implicit none

type(str_type), allocatable :: suffixes(:)

suffixes = [str_type('str1'), str_type('str2')]
!print *, suffixes(1)%val, suffixes(2)%val
print *, endswith(str_type('str1'), suffixes)

end

If one removes the elemental attribute of the constructor str_maker of the str_type derived type, the code compiles and runs successfully. However, this should not be needed because the code is valid; in practice, str_maker is far more complex than this simple bug reproducer above.

The segmentation fault occurs at line 16 inside the elemental function `endswith`. Here is a link to compile, run, and compare with other compiler outputs: https://godbolt.org/z/j5Eecr6rv

The same segfault error occurs if one uses a container of allocatable scalar other than a string; this error is not bound to strings alone.

Thank you for the impressive compiler and for any help in resolving this bug (or error).

0 Kudos
0 Replies
Reply