[wplug] C compiling question

Matthew Danish mdanish at andrew.cmu.edu
Tue Oct 1 08:01:01 EDT 2002


On Tue, Oct 01, 2002 at 07:21:51AM -0400, Rafael E. Herrera wrote:
> James O'Kane wrote:
> > Did something like this ever work as expected with any version of gcc? I
> > found this code in mod_throttle, an apache module to add quotas to remote
> > users. I'm surprised that it is being distributed because at least for me,
> > it doesn't do what I would want. I've changed things to strcmp(), so I'm
> > all set in terms of getting it to work, but I'm curious if gcc ever
> > compiled this so it would work?
> > 
> > 
> > static const char true[] = "true";
> > char *foo = NULL;
> > 
> > foo = setfoo();
> > 
> > if(foo == true){
> > // do stuff if foo is true
> > }
> 
> 
> If the idea is to check whether setfoo() returned successfully, that 
> piece of code is really wrong or just very poor coding style.
> 
> If a function needs to return a success status, make it just return a 0 
> or 1.

It's quite safe, since it's just comparing "object identity" which in C
would boil down to pointer identity.  He's defined the `true' object and
bound it to a constant variable.  OTOH, I wouldn't count on the identity
of the `true' object remaining the same through multiple invocations of
the program, so it definitely shouldn't be stored in any fashion.  It
must be stored by name rather than by identity, in other words.  Quite
similar to how true/false boolean types are implemented in some
higher-level languages.

Actually the only possible gotcha with the code is whether or not the
compiler is allowed to generate different objects for the char true[]
constant, and possibly fold them into the various usages of the
constant.  Certainly, if the line means that the pointer value must be
constant, then it is safe.  But if the line only means that the string
remains constant (ie. copies are permitted) then it would become a
problem.  But knowing C, I'd bet on the former =)

As for 0 and 1, there are many C functions which return 1 on success,
many that return 0 on success, many that return -1 on success, and many
that just do whatever the hell pleases them.  It's rather a unfortunate
situation, really.

-- 
; Matthew Danish <mdanish at andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."



More information about the wplug mailing list