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; } } }