public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/106463] New: Incorrect value for loop terminating test. for loop runs when it should not.
@ 2022-07-28 11:19 gordon.lack at dsl dot pipex.com
  2022-07-28 11:20 ` [Bug c/106463] " gordon.lack at dsl dot pipex.com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gordon.lack at dsl dot pipex.com @ 2022-07-28 11:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106463
           Summary: Incorrect value for loop terminating test. for loop
                    runs when it should not.
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gordon.lack at dsl dot pipex.com
  Target Milestone: ---

Created attachment 53371
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53371&action=edit
Preprocessed .i file

For the simple loop:

    int maxnum = power10(n);
    for (int unum = 0; unum < maxnum; unum++) {
        some code...
    }

the loop will run (multiple times) even if maxnum is -ve (a result of an
overflow in powr10).

This happens for -O3 and -O2, but not -O1 or -O0 using gcc11.2.0 (on Kubuntu
22.04).
Also happens on gcc:
   10.2.1
    7.4.0

On 4.4.7 (Centos6) it only passes through the loop once (which is still wrong).

The raw test.c file to show the problem is short:
==========================================================
#include <stdlib.h>
#include <string.h>

static int power10(int exp) {
    int res = 1;
    while (exp-- > 0) res *= 10;
    return res;   
}

static char testname[32] = "test";
static int namelen = 4;
static char name[32];

void *bfind(char *fn, int cflag, int bflag) {
    snprintf(fn, 32, "%d %d", cflag, bflag);
    return fn;
}

int main(int argc, char *argv[]) {
    if (!argv[1]) return 2;
    int numlen = atoi(argv[1]);
    int maxnum = power10(numlen);
    printf("Loop max %d\n", maxnum);
    for (int unum = 0; unum < maxnum; unum++) {
        printf("Loop var %d (max %d)\n", unum, maxnum);
        printf("Loop end cond %d\n", (unum < maxnum));
        snprintf(testname + namelen, 28, "%d", unum);
        if (bfind(testname, 0, 0) == NULL) {
            strcpy(name, testname);
            return 0;
        }
    }
    return 1;
}

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

* [Bug c/106463] Incorrect value for loop terminating test. for loop runs when it should not.
  2022-07-28 11:19 [Bug c/106463] New: Incorrect value for loop terminating test. for loop runs when it should not gordon.lack at dsl dot pipex.com
@ 2022-07-28 11:20 ` gordon.lack at dsl dot pipex.com
  2022-07-28 11:36 ` gordon.lack at dsl dot pipex.com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: gordon.lack at dsl dot pipex.com @ 2022-07-28 11:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Gordon Lack <gordon.lack at dsl dot pipex.com> ---
Sorry. The first line of test.c was missing in that cut&paste.
There's a missing:

#include <stdio.h>

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

* [Bug c/106463] Incorrect value for loop terminating test. for loop runs when it should not.
  2022-07-28 11:19 [Bug c/106463] New: Incorrect value for loop terminating test. for loop runs when it should not gordon.lack at dsl dot pipex.com
  2022-07-28 11:20 ` [Bug c/106463] " gordon.lack at dsl dot pipex.com
@ 2022-07-28 11:36 ` gordon.lack at dsl dot pipex.com
  2022-07-28 12:01 ` pinskia at gcc dot gnu.org
  2022-07-28 16:43 ` gordon.lack at dsl dot pipex.com
  3 siblings, 0 replies; 5+ messages in thread
From: gordon.lack at dsl dot pipex.com @ 2022-07-28 11:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Gordon Lack <gordon.lack at dsl dot pipex.com> ---
Compilation options:
gcc -O3 test.c -o test
(or -O1)

For gcc 4.4.7 it also needs -std=c99.

And I'll also note that the gcc10.2.1 was on an armv7l system, while the 7.4.0
was on an arm64 one.
It's also the same on 7.20 on a mips system.

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

* [Bug c/106463] Incorrect value for loop terminating test. for loop runs when it should not.
  2022-07-28 11:19 [Bug c/106463] New: Incorrect value for loop terminating test. for loop runs when it should not gordon.lack at dsl dot pipex.com
  2022-07-28 11:20 ` [Bug c/106463] " gordon.lack at dsl dot pipex.com
  2022-07-28 11:36 ` gordon.lack at dsl dot pipex.com
@ 2022-07-28 12:01 ` pinskia at gcc dot gnu.org
  2022-07-28 16:43 ` gordon.lack at dsl dot pipex.com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-28 12:01 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Signed integer overflow is undefined. 
Use -fsanitize=undefined to detect this st runtime.

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

* [Bug c/106463] Incorrect value for loop terminating test. for loop runs when it should not.
  2022-07-28 11:19 [Bug c/106463] New: Incorrect value for loop terminating test. for loop runs when it should not gordon.lack at dsl dot pipex.com
                   ` (2 preceding siblings ...)
  2022-07-28 12:01 ` pinskia at gcc dot gnu.org
@ 2022-07-28 16:43 ` gordon.lack at dsl dot pipex.com
  3 siblings, 0 replies; 5+ messages in thread
From: gordon.lack at dsl dot pipex.com @ 2022-07-28 16:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Gordon Lack <gordon.lack at dsl dot pipex.com> ---
OK. Agreed.
It's in the C99 standard that signed integer overflow is undefined behaviour.
Thanks for the reply.

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

end of thread, other threads:[~2022-07-28 16:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-28 11:19 [Bug c/106463] New: Incorrect value for loop terminating test. for loop runs when it should not gordon.lack at dsl dot pipex.com
2022-07-28 11:20 ` [Bug c/106463] " gordon.lack at dsl dot pipex.com
2022-07-28 11:36 ` gordon.lack at dsl dot pipex.com
2022-07-28 12:01 ` pinskia at gcc dot gnu.org
2022-07-28 16:43 ` gordon.lack at dsl dot pipex.com

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).