]> 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 13:41:10 +0000 (15:41 +0200)
commitc84a951a8ee151cb4d80dd84f75f421c5f60fa05
tree608d98c316466c21d33b80b45ad6e82514d36d00
parentad998962ccc8cd190da2d85f0274bbb7bb9f6cae
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.

(cherry picked from commit 748a7bc4624e7b000f6fdb93a8cf7da73ff193bb)
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