public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time
@ 2022-05-22 14:44 xecej4 at outlook dot com
  2022-05-22 14:48 ` [Bug fortran/105691] " xecej4 at outlook dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: xecej4 at outlook dot com @ 2022-05-22 14:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

            Bug ID: 105691
           Summary: Incorrect calculation of INDEX(str1,str2) at compile
                    time
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xecej4 at outlook dot com
  Target Milestone: ---

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
@ 2022-05-22 14:48 ` xecej4 at outlook dot com
  2022-05-22 18:08 ` kargl at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: xecej4 at outlook dot com @ 2022-05-22 14:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #1 from mecej4 <xecej4 at outlook dot com> ---
When a program contains an expression that involves the INDEX intrinsic
function, and it is possible to calculate its result value at compile time, the
computed value is sometimes incorrect.

program main
   i = index("fortran.f90", "fortran", back=.true.) ! It should get '1'
   call prnt(i)
contains
   subroutine prnt(i)
   integer i
   print *,'i = ',i
   end subroutine
end program

The program prints '0' instead of '1'. Looking at the assembly code generated
shows that the argument value in the argument I passed to PRNT() is zero.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
  2022-05-22 14:48 ` [Bug fortran/105691] " xecej4 at outlook dot com
@ 2022-05-22 18:08 ` kargl at gcc dot gnu.org
  2022-05-22 18:33 ` kargl at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kargl at gcc dot gnu.org @ 2022-05-22 18:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-05-22
                 CC|                            |kargl at gcc dot gnu.org
     Ever confirmed|0                           |1
           Priority|P3                          |P4
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from kargl at gcc dot gnu.org ---
There is some torched logic looking for the substring, which can never find it.

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 233cc42137f..16b231f5707 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3606,32 +3606,29 @@ gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr
*b, gfc_expr *kind)
        }
       else
        {
-         for (i = 0; i < len; i++)
+         /* Start at the tail of the string, offset by the length of the
+            substring, and search for a match moving towards the head of
+            string.  */
+         for (i = len - lensub; i >= 0; i--)
            {
-             for (j = 0; j < lensub; j++)
+             /* If the first character does not match, then the rest of the
+                string cannot match.  */
+             if (x->value.character.string[i]
+                 == y->value.character.string[0])
                {
-                 if (y->value.character.string[j]
-                     == x->value.character.string[len - i])
+
+                 /* Compare substring to starting location in string.  */      
+                 for (j = 0, count = 0; j < lensub; j++, count++)
                    {
-                     start = len - i;
-                     if (start <= len - lensub)
-                       {
-                         count = 0;
-                         for (k = 0; k < lensub; k++)
-                           if (y->value.character.string[k]
-                               == x->value.character.string[k + start])
-                             count++;
-
-                         if (count == lensub)
-                           {
-                             index = start + 1;
-                             goto done;
-                           }
-                       }
-                     else
-                       {
-                         continue;
-                       }
+                     if (x->value.character.string[i + j]
+                         != y->value.character.string[j])
+                       break;
+                   }
+
+                 if (count == lensub)
+                   {
+                     index = i + 1;
+                     goto done;
                    }
                }
            }

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
  2022-05-22 14:48 ` [Bug fortran/105691] " xecej4 at outlook dot com
  2022-05-22 18:08 ` kargl at gcc dot gnu.org
@ 2022-05-22 18:33 ` kargl at gcc dot gnu.org
  2022-06-21 20:24 ` anlauf at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kargl at gcc dot gnu.org @ 2022-05-22 18:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #3 from kargl at gcc dot gnu.org ---
There is no need to special case the substring length of 1 case with my
suggested patch.  Here's an update to eliminate the special case.

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 233cc42137f..fa9938d6a1e 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3589,49 +3589,31 @@ gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr
*b, gfc_expr *kind)
          mpz_set_si (result->value.integer, len + 1);
          return result;
        }
-      else if (lensub == 1)
+      else
        {
-         for (i = 0; i < len; i++)
+         /* Start at the tail of the string, offset by the length of the
+            substring, and search for a match moving towards the head of
+            string.  */
+         for (i = len - lensub; i >= 0; i--)
            {
-             for (j = 0; j < lensub; j++)
+             /* If the first character does not match, then the rest of the
+                string cannot match.  */
+             if (x->value.character.string[i]
+                 == y->value.character.string[0])
                {
-                 if (y->value.character.string[j]
-                     == x->value.character.string[len - i])
+
+                 /* Compare substring to starting location in string.  */      
+                 for (j = 0, count = 0; j < lensub; j++, count++)
                    {
-                     index = len - i + 1;
-                     goto done;
+                     if (x->value.character.string[i + j]
+                         != y->value.character.string[j])
+                       break;
                    }
-               }
-           }
-       }
-      else
-       {
-         for (i = 0; i < len; i++)
-           {
-             for (j = 0; j < lensub; j++)
-               {
-                 if (y->value.character.string[j]
-                     == x->value.character.string[len - i])
+
+                 if (count == lensub)
                    {
-                     start = len - i;
-                     if (start <= len - lensub)
-                       {
-                         count = 0;
-                         for (k = 0; k < lensub; k++)
-                           if (y->value.character.string[k]
-                               == x->value.character.string[k + start])
-                             count++;
-
-                         if (count == lensub)
-                           {
-                             index = start + 1;
-                             goto done;
-                           }
-                       }
-                     else
-                       {
-                         continue;
-                       }
+                     index = i + 1;
+                     goto done;
                    }
                }
            }

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (2 preceding siblings ...)
  2022-05-22 18:33 ` kargl at gcc dot gnu.org
@ 2022-06-21 20:24 ` anlauf at gcc dot gnu.org
  2022-06-21 21:28 ` anlauf at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-06-21 20:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |anlauf at gcc dot gnu.org
           Assignee|unassigned at gcc dot gnu.org      |anlauf at gcc dot gnu.org

--- Comment #4 from anlauf at gcc dot gnu.org ---
(In reply to kargl from comment #3)
> There is no need to special case the substring length of 1 case with my
> suggested patch.  Here's an update to eliminate the special case.

Frankly speaking, gfc_simplify_index is a mess.  The library code is a relief.
I'd take that as reference.

Thus taking.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (3 preceding siblings ...)
  2022-06-21 20:24 ` anlauf at gcc dot gnu.org
@ 2022-06-21 21:28 ` anlauf at gcc dot gnu.org
  2022-06-21 21:31 ` sgk at troutmask dot apl.washington.edu
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-06-21 21:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #5 from anlauf at gcc dot gnu.org ---
Submitted version: https://gcc.gnu.org/pipermail/fortran/2022-June/057940.html

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (4 preceding siblings ...)
  2022-06-21 21:28 ` anlauf at gcc dot gnu.org
@ 2022-06-21 21:31 ` sgk at troutmask dot apl.washington.edu
  2022-06-26 20:07 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2022-06-21 21:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #6 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Tue, Jun 21, 2022 at 09:28:27PM +0000, anlauf at gcc dot gnu.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691
> 
> --- Comment #5 from anlauf at gcc dot gnu.org ---
> Submitted version: https://gcc.gnu.org/pipermail/fortran/2022-June/057940.html
> 

Harald, looks like a straight forward translation of the 
library algorithm.  OK to commit.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (5 preceding siblings ...)
  2022-06-21 21:31 ` sgk at troutmask dot apl.washington.edu
@ 2022-06-26 20:07 ` cvs-commit at gcc dot gnu.org
  2022-06-26 20:12 ` anlauf at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-26 20:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd

commit r13-1277-gff35dbc02092fbcd3d814fcd9fe8e871c3f741fd
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 21 23:20:18 2022 +0200

    Fortran: fix simplification of INDEX(str1,str2) [PR105691]

    gcc/fortran/ChangeLog:

            PR fortran/105691
            * simplify.cc (gfc_simplify_index): Replace old simplification
            code by the equivalent of the runtime library implementation.  Use
            HOST_WIDE_INT instead of int for string index, length variables.

    gcc/testsuite/ChangeLog:

            PR fortran/105691
            * gfortran.dg/index_6.f90: New test.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (6 preceding siblings ...)
  2022-06-26 20:07 ` cvs-commit at gcc dot gnu.org
@ 2022-06-26 20:12 ` anlauf at gcc dot gnu.org
  2022-06-30 20:05 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-06-26 20:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #8 from anlauf at gcc dot gnu.org ---
Fixed on mainline so far.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (7 preceding siblings ...)
  2022-06-26 20:12 ` anlauf at gcc dot gnu.org
@ 2022-06-30 20:05 ` cvs-commit at gcc dot gnu.org
  2022-07-01 17:54 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-30 20:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Harald Anlauf
<anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:26ea506a1e8719f8b1f559e70bee9f5d3392eb37

commit r12-8530-g26ea506a1e8719f8b1f559e70bee9f5d3392eb37
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 21 23:20:18 2022 +0200

    Fortran: fix simplification of INDEX(str1,str2) [PR105691]

    gcc/fortran/ChangeLog:

            PR fortran/105691
            * simplify.cc (gfc_simplify_index): Replace old simplification
            code by the equivalent of the runtime library implementation.  Use
            HOST_WIDE_INT instead of int for string index, length variables.

    gcc/testsuite/ChangeLog:

            PR fortran/105691
            * gfortran.dg/index_6.f90: New test.

    (cherry picked from commit ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (8 preceding siblings ...)
  2022-06-30 20:05 ` cvs-commit at gcc dot gnu.org
@ 2022-07-01 17:54 ` cvs-commit at gcc dot gnu.org
  2022-07-01 17:55 ` cvs-commit at gcc dot gnu.org
  2022-07-01 17:57 ` anlauf at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-07-01 17:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Harald Anlauf
<anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:614d9e76b71df6c5ad42f2b9c2a8156e8b3ebd35

commit r11-10102-g614d9e76b71df6c5ad42f2b9c2a8156e8b3ebd35
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 21 23:20:18 2022 +0200

    Fortran: fix simplification of INDEX(str1,str2) [PR105691]

    gcc/fortran/ChangeLog:

            PR fortran/105691
            * simplify.c (gfc_simplify_index): Replace old simplification
            code by the equivalent of the runtime library implementation.  Use
            HOST_WIDE_INT instead of int for string index, length variables.

    gcc/testsuite/ChangeLog:

            PR fortran/105691
            * gfortran.dg/index_6.f90: New test.

    (cherry picked from commit ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (9 preceding siblings ...)
  2022-07-01 17:54 ` cvs-commit at gcc dot gnu.org
@ 2022-07-01 17:55 ` cvs-commit at gcc dot gnu.org
  2022-07-01 17:57 ` anlauf at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-07-01 17:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Harald Anlauf
<anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:e6db08d9183b80b0ada5122fae79412544279f56

commit r10-10877-ge6db08d9183b80b0ada5122fae79412544279f56
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Jun 21 23:20:18 2022 +0200

    Fortran: fix simplification of INDEX(str1,str2) [PR105691]

    gcc/fortran/ChangeLog:

            PR fortran/105691
            * simplify.c (gfc_simplify_index): Replace old simplification
            code by the equivalent of the runtime library implementation.  Use
            HOST_WIDE_INT instead of int for string index, length variables.

    gcc/testsuite/ChangeLog:

            PR fortran/105691
            * gfortran.dg/index_6.f90: New test.

    (cherry picked from commit ff35dbc02092fbcd3d814fcd9fe8e871c3f741fd)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug fortran/105691] Incorrect calculation of INDEX(str1,str2) at compile time
  2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
                   ` (10 preceding siblings ...)
  2022-07-01 17:55 ` cvs-commit at gcc dot gnu.org
@ 2022-07-01 17:57 ` anlauf at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-07-01 17:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105691

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |10.5
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #12 from anlauf at gcc dot gnu.org ---
Fixed for all open branches.  Closing.

Thanks for the report!

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-07-01 17:57 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-22 14:44 [Bug fortran/105691] New: Incorrect calculation of INDEX(str1,str2) at compile time xecej4 at outlook dot com
2022-05-22 14:48 ` [Bug fortran/105691] " xecej4 at outlook dot com
2022-05-22 18:08 ` kargl at gcc dot gnu.org
2022-05-22 18:33 ` kargl at gcc dot gnu.org
2022-06-21 20:24 ` anlauf at gcc dot gnu.org
2022-06-21 21:28 ` anlauf at gcc dot gnu.org
2022-06-21 21:31 ` sgk at troutmask dot apl.washington.edu
2022-06-26 20:07 ` cvs-commit at gcc dot gnu.org
2022-06-26 20:12 ` anlauf at gcc dot gnu.org
2022-06-30 20:05 ` cvs-commit at gcc dot gnu.org
2022-07-01 17:54 ` cvs-commit at gcc dot gnu.org
2022-07-01 17:55 ` cvs-commit at gcc dot gnu.org
2022-07-01 17:57 ` anlauf at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).