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)
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.
Indeed, the bug is fixed in the rawhide gcc, and furthemore, teTeX compiles and runs fine with it.