image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old December 12th, 2005, 02:02 PM
Registered User
 
Join Date: Mar 2002
Location: Kyoto, Japan
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Fusengammu is on a distinguished road
Did I just find a bug in gcc/g++?

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
Reply With Quote
  #2  
Old December 12th, 2005, 05:53 PM
ElDiabloConCaca's Avatar
U.S.D.A. Prime
 
Join Date: Aug 2001
Location: San Antonio, Texas
Posts: 10,688
Thanks: 3
Thanked 162 Times in 150 Posts
ElDiabloConCaca is a jewel in the roughElDiabloConCaca is a jewel in the roughElDiabloConCaca is a jewel in the rough
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.
__________________
Power Macintosh G4/500MHz "Yikes!" 10.4.11 Server • 1024MB • 3 x 120GB + 320GB • DVR-111D
MacBook 2.0GHz Core 2 Duo - White 10.5.6 • 2048MB • 80GB • CD-RW/DVD-ROM
iPhone 3G 8GB • iPod Photo 60GB • iPod nano 1GB • AT&T DSL 6Mb/768k
http://www.jeffhoppe.com

Last edited by ElDiabloConCaca; December 12th, 2005 at 06:05 PM.
Reply With Quote
  #3  
Old December 12th, 2005, 11:38 PM
Registered User
 
Join Date: Mar 2002
Location: Kyoto, Japan
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Fusengammu is on a distinguished road
local macros

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
Reply With Quote
  #4  
Old December 13th, 2005, 12:11 AM
ElDiabloConCaca's Avatar
U.S.D.A. Prime
 
Join Date: Aug 2001
Location: San Antonio, Texas
Posts: 10,688
Thanks: 3
Thanked 162 Times in 150 Posts
ElDiabloConCaca is a jewel in the roughElDiabloConCaca is a jewel in the roughElDiabloConCaca is a jewel in the rough
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.
__________________
Power Macintosh G4/500MHz "Yikes!" 10.4.11 Server • 1024MB • 3 x 120GB + 320GB • DVR-111D
MacBook 2.0GHz Core 2 Duo - White 10.5.6 • 2048MB • 80GB • CD-RW/DVD-ROM
iPhone 3G 8GB • iPod Photo 60GB • iPod nano 1GB • AT&T DSL 6Mb/768k
http://www.jeffhoppe.com
Reply With Quote
  #5  
Old December 13th, 2005, 01:03 AM
Registered User
 
Join Date: Mar 2002
Location: Kyoto, Japan
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Fusengammu is on a distinguished road
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!
Reply With Quote
  #6  
Old December 13th, 2005, 02:50 AM
Viro's Avatar
Registered User
 
Join Date: Nov 2003
Location: Oxford, UK
Posts: 2,494
Thanks: 0
Thanked 1 Time in 1 Post
Viro will become famous soon enoughViro will become famous soon enough
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.
Reply With Quote
  #7  
Old December 13th, 2005, 11:55 AM
Registered User
 
Join Date: Mar 2002
Location: Kyoto, Japan
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Fusengammu is on a distinguished road
thanks, will get adc membership!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 05:48 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.