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

            Bug ID: 97627
           Summary: loop end condition missing - endless loop
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marcin.jachu19 at gmail dot com
  Target Milestone: ---

Created attachment 49467
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49467&action=edit
cpp source file

It seems a loop end condition is missing in this example. Observed on gcc 9.x
and 10.x (all versions). 
This example may look as not much sense, but it is extracted from bigger system
to just explore the issue.

GCC version (in practice any 9.x and 10.x):
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-9.2.0/configure --enable-languages=c,c++
--enable-multilib
Thread model: posix
gcc version 9.2.0 (GCC)
GNU C++17 (GCC) version 9.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 9.2.0, GMP version 4.3.1, MPFR version 2.4.1,
MPC version 0.8, isl version none

System type (doesn't matter, any Linux, for this example used):
centos-release-7-7.1908.0.el7.centos.x86_64

GCC compilation options:
../gcc-9.2.0/configure --enable-languages=c,c++ --enable-multilib

Test compilation options:
g++ -Wall -Wextra -fPIC -O2 -std=c++17 -o test test.cpp

Compiler output:
(none)

Source files attached, the problematic loop:

for (int h = 0; h < num_of_repeats_h; h++)
{
    for (int w = 0; w < num_of_repeats_w; w++)
    {
        printf("w: %d, num_of_repeats_w: %d\n", w, num_of_repeats_w);
    }
}

Expected behavior:
test enters to the inter loop only once, printing counter always equal 0 like:
w: 0, num_of_repeats_w: 1

Actual behavior:
the internal loop never ends, the counter reaches high numbers like:
w: 22373, num_of_repeats_w: 1

Command line
./test
Ctrl+c

More comments:
- gcc 8.x works OK on this example
- it's worth to mention, that -fwrapv option makes it working fine, but I
believe the issue just hides.
- also the -O1 option makes the difference.
- also the -fsanitize=undefined option makes the difference like:
<unknown>: runtime error: execution reached an unreachable program point
but I still believe the source code is correct and the gcc causes the issue.

Reply via email to