Bug 22253 - O2 bug with unsigned type
Summary: O2 bug with unsigned type
Keywords:
Status: CLOSED RAWHIDE
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 7.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
URL: http://thales.memphis.edu/~tetex/try5.c
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2000-12-13 17:14 UTC by mw
Modified: 2007-04-18 16:30 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2000-12-13 17:14:31 UTC
Embargoed:


Attachments (Terms of Use)

Description mw 2000-12-13 17:14:26 UTC
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

http://thales.memphis.edu/~tetex/try4.c)


#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++)
    putchar(name[i]);
  putchar('\n');

  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
/p/q/a-

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

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

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 

http://thales.memphis.edu/~tetex/try5.c

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
/p/q/a-

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 20:48:49 UTC
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 05:34:09 UTC
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.