[wplug] Linux vs OS X version of GCC

Bill Moran wmoran at potentialtech.com
Thu Apr 20 07:05:03 EDT 2006


On Thu, 20 Apr 2006 01:30:35 -0400
Logan <ascii at psu.edu> wrote:

> For class, I've been using my OS X laptop to do a bunch of  
> assignments, but a recent one had a Segmentation Fault when the  
> professor ran it on the Linux server. We had to modify that lab, so I  
> found the error, here's the offending line:
> 
> args[arg_count][strlen(args[arg_count++])] = 0;//null terminate (with  
> a self-referential strlen())
> 	/* ok, here was the bug in Mac vs linux
> 	*  I had "args[arg_count++][strlen(args[arg_count])] = 0;"
> 	*  You can see the change above, apparently Linux increments my  
> arg_count before getting that second []
> 	*  and mac gets both [][] before incrementing arg_count.
> 	*  Either that, or it's gcc version 4.0.0 vs version 3.2.3.
> 	*  Thus Linux segfaults when we try to get strlen(args[invalid]).
> 	*  I thought C was supposed to be portable. Who's correct here?
> 	*/
> 
> So which is correct? Or is this an ambiguity of C, that I should just  
> avoid?

The assumption you are making is that [arg_count] is being evaluated
_before_ [strlen(args[arg_count++])].  I'm not sure this is an assumption
you can safely make, and I expect it's the ambiguity you speak of.

I don't have my copy of K&RC with me, or I'd have a look to see what it
says.  I have a hard time believing that this wouldn't be specified.

> The code there works in both versions of gcc, so I suppose if I just  
> increment on the second [] it's "portable enough" for class ;-)
> I'd still like to know about the first argument bit though.
> 
> The Linux server runs gcc version 3.2.3 20030502 (Red Hat Linux  
> 3.2.3-54).
> My Mac runs gcc version 4.0.0 20041026 (Apple Computer, Inc. build  
> 4061). - I believe that once warned me that this version generates  
> broken code on occasion.

A number of things are possible here:
1) One or the other versions of GCC is buggy.
2) The Mac version has been patched, and doesn't exactly match the GCC
   version in it's behaviour.
3) Have you checked to see what -O level is being used on each system?
   It's fairly well known that higher -O levels are likely to generate
   broken code.

My recommendation would be:
1) Recompile on both systems with -O0 and see if the discrepency is
   still there.
2) If so, look up what the correct behaviour should be in the spec.
3) Report a bug to the appropriate people.

-- 
Bill Moran
Potential Technologies
http://www.potentialtech.com


More information about the wplug mailing list