Did I just find a bug in gcc/g++?

Fusengammu

Registered
Hello, I'm doing some calculations for my master's thesis, and I think I found a bug in gcc/g++. So can some knowledgeable person tell me whether it is/isn't a bug, and if so, should I report it to Apple?

So I boiled the problem down to 2 simple programs, one buggy and the other not:



fileTestB <filename> <param which causes bug>
and
fileTestNB <filename> <param which doesn't causes bug>





The program just tells you whether filename exists or not. fileTestB (the buggy one) tells me that a non-existent file exists if I give the third argument, while fileTestNB (Not buggy) tells me that a non-existent file doesn't exist whether I give the third param or not.




The buggy output looks like: (a file called "foo" doesn't exist)


****** ~ $ fileTestB foo argv3
****** PRINTING foo CAUSES BUG
****** foo is a regular file
****** ~ $ fileTestB foo
****** foo is a NOT a regular file


while the non-buggy output looks like


****** ~ $ fileTestNB foo argv3
****** PRINTING foo CAUSES NO BUG
****** foo is a NOT a regular file
****** ~ $ fileTestNB foo
****** foo is a NOT a regular file




The source code for the buggy and non-buggy programs are:



/******** begin code for "fileTestB" ********/
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>

int fexists(char *);

int main(int argc, char** argv)
{
if( argc < 2 )
{
fprintf(stderr, "fileTests <file or dir name> <3rd arg causes BUG>¥n");
exit(1);
}

if( argc == 3 )
{
// giving a 3rd argument causes BUG
printf(" PRINTING %s CAUSES BUG¥n", argv[1]);
}

if( fexists(argv[1]) )
{
printf("%s is a regular file¥n", argv[1]);
}
else
{
printf("%s is a NOT a regular file¥n", argv[1]);
}
}


int fexists(char *fname)
{
struct stat buffer;
stat(fname, &buffer);
return S_ISREG(buffer.st_mode);
}
/******** end code for "fileTestB" ********/






/******** begin code for "fileTestNB" ********/
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>

int main(int argc, char** argv)
{
if( argc < 2 )
{
fprintf(stderr, "fileTests <file or dir name> <3rd arg causes NO BUG>¥n");
exit(1);
}

if( argc == 3 )
{
// giving a 3rd argument causes NO BUG
printf(" PRINTING %s CAUSES NO BUG¥n", argv[1]);
}

struct stat buffer;
stat(argv[1], &buffer);

if( S_ISREG(buffer.st_mode) )
{
printf("%s is a regular file¥n", argv[1]);
}
else
{
printf("%s is a NOT a regular file¥n", argv[1]);
}
}
/******** end code for "fileTestB" ********/



I'm using OS X 10.3.9 on a Dual 2.5GHz G5.


thanx
 
They worked just fine for me (meaning both programs correctly identified whether a file was a regular file or not, both with and without the 3rd argument), with a small change:

#include <cstdio>
#include <cstdlib>

to:

#include <stdio.h>
#include <stdlib.h>

The problem, I think, is that S_ISREG is a macro, so the scope of it needs to be clear. It seems that you cannot use a local macro in certain situations, as it will always return nil or something to that effect, but I can't remember the specifics of it. Also, S_ISREG() is not always defined on all systems, but that wouldn't explain why one correctly runs and the other doesn't.

Using gcc 4.0.1 on Tiger 10.4.3, G4 500MHz.
 
yeah, i forgot to mention, i am using g++, hence the

#include <cstdio>


the thing that i don't understand is that on my linux machine, the code above works correctly, so it gives a different result than on my mac. and i thought as long as you were able to see a macro (meaning if it compiles), you could use it? are there more rules that have to be followed? because it seems like the macros work, but calling them from inside a function, they stop working (is this the scoping you were talking about?) all very confusing.

thankx though
 
Hmmm... strange. I compiled your source again as-is with g++, and both programs work correctly on my machine.

Did you install g++ yourself, or have you installed the Apple Developer Tools/XCode?

I'm using g++ 4.0.1.
 
Yeah, I installed Xcode (1.2) that came with my Panther install CD. Do you know if you have to be an ADC member to install Xcode updates?

thanx mr. concaca!
 
You have to be an ADC member, but the cost of registration is free. That should allow you to download XCode 1.5 for Panther.
 
Back
Top