Bug 22253 - O2 bug with unsigned type
O2 bug with unsigned type
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
David Lawrence
Depends On:
  Show dependency treegraph
Reported: 2000-12-13 12:14 EST by mw
Modified: 2007-04-18 12:30 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2000-12-13 12:14:31 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description mw 2000-12-13 12:14:26 EST
I noticed this one while trying to install teTeX-1.0.  It compiles fine,
but the program does not work.  I isolated the problem, and the following 
program will show the error (it is available as


#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])

  char *name;
  int loc;
  unsigned limit; 
  int i;
  name = argv[1];
  limit = 0;

  if (!name)
      puts("name ?");
      return 0;
  for (loc = strlen (name); (loc > limit) && (name[loc-1] != '/'); loc--)
  for (i = 0; i < loc - 1; i++)

  return 0;

This program should find dirname of argv[1].  But I get incorrect output if
I compile it with `-O2':

$ gcc -Wall -O2 try4.c
$ ./a.out /p/q/a-b-c

I get the correct output if I just use `-O' optimization:

$ gcc -Wall -O try4.c
$ ./a.out /p/q/a-b-c

The problem somehow seems to be with declaring `limit' to be "unsigned". If
I change the declaration of limit to 

unsigned int limit;

all is well.

The problem is that the `for' loop terminates prematurely---even though the
value of 

(loc > limit) && (name[loc-1] != '/')

is one.  To test this, I made a test version of the program, and put teh
source at 


In try5.c, I put 

  test1 = (loc > limit);
  test2 = ( name[loc - 1] != '/' );
  test3 = (loc > limit) && ( name[loc - 1] != '/' );
  printf("test1: %d; test2: %d; test3: %d\n", test1, test2, test3);

after the for loop.  Here is what I get

$ gcc -Wall -O2 try5.c
$ ./a.out /p/q/a-b-c
test1: 1; test2: 1; test3: 1

Finally, let me point out that if I make any of the changes below in
try4.c, the program works correctly:

-- change (loc > limit)  to (limit < loc)

-- change (loc > limit) to (loc > 0)
Comment 1 Jakub Jelinek 2000-12-13 15:48:49 EST
I believe this is already fixed in gcc-2.96-65, at least I can
reproduce it with gcc-2.96-63 and cannot with -65.
Comment 2 mw 2000-12-14 00:34:09 EST
Indeed, the bug is fixed in the rawhide gcc, and furthemore, teTeX
compiles and runs fine with it.

Note You need to log in before you can comment on or make changes to this bug.