]> git.ipfire.org Git - thirdparty/gcc.git/commit
libfortran: Fix up _gfortran_{,m,s}findloc2_s{1,4} [PR120196]
authorJakub Jelinek <jakub@redhat.com>
Tue, 13 May 2025 12:20:22 +0000 (14:20 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 13 May 2025 12:25:38 +0000 (14:25 +0200)
commit748a7bc4624e7b000f6fdb93a8cf7da73ff193bb
treed1952d626cf65c6f24a95952b0a2cee0149c7ae2
parent781cfc454b8dc24952fe7f4c5c409296dca505e1
libfortran: Fix up _gfortran_{,m,s}findloc2_s{1,4} [PR120196]

As mentioned in the PR, _gfortran_{,m,s}findloc2_s{1,4} iterate too many
times in the back case if nothing is found.
For !back, the loops are for (i = 1; i <= extent; i++) so i is in the
body [1, extent] if nothing is found, but for back it is
for (i = extent; i >= 0; i--) so i is in the body [0, extent] and compares
one element before the start of the array.
Note, findloc1_s{1,4} uses
          for (n = len; n > 0; n--, src -= delta * len_array)
for the back loop and
          for (n = 1; n <= len; n++, src += delta * len_array)
for !back.  This patch fixes that.
The testcase fails under valgrind without the libgfortran changes and
succeeds with those.

2025-05-13  Jakub Jelinek  <jakub@redhat.com>

PR libfortran/120196
* m4/ifindloc2.m4 (header1, header2): For back use i > 0 rather than
i >= 0 as for condition.
* generated/findloc2_s1.c: Regenerate.
* generated/findloc2_s4.c: Regenerate.

* gfortran.dg/pr120196.f90: New test.
gcc/testsuite/gfortran.dg/pr120196.f90 [new file with mode: 0644]
libgfortran/generated/findloc2_s1.c
libgfortran/generated/findloc2_s4.c
libgfortran/m4/ifindloc2.m4