fortran-lang / stdlib

Fortran Standard Library
https://stdlib.fortran-lang.org
MIT License
1.09k stars 168 forks source link

Test: string_intrinsic (move) failed built with GCC 13.2 #731

Closed band-a-prend closed 1 year ago

band-a-prend commented 1 year ago

Description

If Fortran stdlib build with GCC 13.2 when on running test the string_intrinsic test fail with error (debug mode):

38/52 Test: string_intrinsic
Command: "/var/tmp/portage/dev-libs/fortran-stdlib-0.2.1-r1/work/stdlib-0.2.1_build/src/tests/string/test_string_intrinsic" "/var/tmp/portage/dev-libs/fortran-stdlib-0.2.1-r1/work/stdlib-0.2.1_build/src/tests/string"
Directory: /var/tmp/portage/dev-libs/fortran-stdlib-0.2.1-r1/work/stdlib-0.2.1/src/tests/string
"string_intrinsic" start time: Jul 23 19:27 MSK
Output:
----------------------------------------------------------
# Testing: string-intrinsic
  Starting lgt ... (1/17)
       ... lgt [PASSED]
  Starting llt ... (2/17)
       ... llt [PASSED]
  Starting lge ... (3/17)
       ... lge [PASSED]
  Starting lle ... (4/17)
       ... lle [PASSED]
  Starting trim ... (5/17)
       ... trim [PASSED]
  Starting len ... (6/17)
       ... len [PASSED]
  Starting len_trim ... (7/17)
       ... len_trim [PASSED]
  Starting adjustl ... (8/17)
       ... adjustl [PASSED]
  Starting adjustr ... (9/17)
       ... adjustr [PASSED]
  Starting scan ... (10/17)
       ... scan [PASSED]
  Starting verify ... (11/17)
       ... verify [PASSED]
  Starting repeat ... (12/17)
       ... repeat [PASSED]
  Starting index ... (13/17)
       ... index [PASSED]
  Starting char ... (14/17)
       ... char [PASSED]
  Starting ichar ... (15/17)
       ... ichar [PASSED]
  Starting iachar ... (16/17)
       ... iachar [PASSED]
  Starting move ... (17/17)

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7fe30b93f8e8 in ???
#1  0x7fe30b93ea3d in ???
#2  0x7fe30b67b98f in ???
#3  0x7fe30b6e24fd in ???
#4  0x7fe30bbe85df in make_output
    at /var/tmp/portage/dev-util/fortran-test-drive-0.4.0/work/test-drive-0.4.0/src/testdrive.F90:459
#5  0x7fe30bbe85df in run_unittest
    at /var/tmp/portage/dev-util/fortran-test-drive-0.4.0/work/test-drive-0.4.0/src/testdrive.F90:424
#6  0x7fe30bbef984 in __testdrive_MOD_run_testsuite
    at /var/tmp/portage/dev-util/fortran-test-drive-0.4.0/work/test-drive-0.4.0/src/testdrive.F90:336
#7  0x561ec26e6d70 in tester
    at /var/tmp/portage/dev-libs/fortran-stdlib-0.2.1-r1/work/stdlib-0.2.1/src/tests/string/test_string_intrinsic.f90:745
#8  0x561ec26e445e in main
    at /var/tmp/portage/dev-libs/fortran-stdlib-0.2.1-r1/work/stdlib-0.2.1/src/tests/string/test_string_intrinsic.f90:730
<end of output>
Test time =   0.01 sec
----------------------------------------------------------
Test Failed.

Actually it could be related to https://github.com/fortran-lang/stdlib/blob/df1e2f0ed0cbe2fbd9c1f20dcb5bd1a4bba95bb2/test/string/test_string_intrinsic.f90#L716 because if simple test program

program test

    character(len=:), allocatable :: output
    character(len=:), allocatable :: input
    input = "Test sting"
!    output = "Test sting"
    print *, input
    call move_alloc(input, output)
    print *, output, "DONE1"

    call move_alloc(output, output)
    print *, output, "DONE2"
end program

is tried to compile with GCC 13.2 then the compile error is arrised

example.f90:11:28:

   11 |     call move_alloc(output, output)
      |                            1
Error: The FROM and TO arguments at (1) violate aliasing restrictions (F2003 12.4.1.7)

If compiled with GCC 12.x then there is no error with string_intrinsic test.

If comment the appropriate test (stdlib/test/string/test_string_intrinsic.f90:716-718) then string_intrinsic is passed

!        call move(from_string, from_string)
!        call check(error, from_string == "", "move: test_case 8")
!        if (allocated(error)) return

Expected Behaviour

All tests should be passed.

Version of stdlib

0.2.1, 0.3.0

Platform and Architecture

Gentoo Linux

Additional Information

The build and testing are made with Gentoo package manager to package Fortran stdlib for Gentoo Guru (maintained by users) repository.

Related issue: https://bugs.gentoo.org/910241

jvdp1 commented 1 year ago

Thank you for reporting this issue with a detailed explanation. It seems related to #726 I am not sure why people would aim to move mem from one variable to the same variable.

@awvwgk Any idea on how to solve this issue?Maybe by creating a temp variable?

awvwgk commented 1 year ago

The issue is that we declare the second argument of move as intent(out), the only way to work with this would change the second argument to intent(inout).

band-a-prend commented 1 year ago

Is this string meant? https://github.com/fortran-lang/stdlib/blob/df1e2f0ed0cbe2fbd9c1f20dcb5bd1a4bba95bb2/src/stdlib_string_type.fypp#L683

I tried to replace it with

type(string_type), intent(inout) :: to

but without success for the mentioned test: it still fails with gcc 13.

What about additional check instead smth. like

if (from%raw .neqv. to%raw) call move_alloc(from%raw, to%raw)

Or smth. that check if from and to already use the same memory, e.g. via loc function?

But I need to check results.