[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: glibc/gcc questions



On 16-Apr-99, 22:50 (CDT), David Welton <davidw@master.debian.org> wrote: 
> > On Mon, Apr 12, 1999 at 01:49:03AM -0500, David Welton wrote:
> > > 
> > > strncpy(oldnick, nickname,
> > > #ifdef ALLOW_LONG_NICKNAMES
> > > 		    LONG_NICKNAME_LEN
> > > #else
> > > 		    NICKNAME_LEN
> > > #endif
> > > 		    );
> 
> The problem is that strncpy is defined as a macro.  Hrmm.  I'm not
> sure what to think about this, if the code is buggy, making
> assumptions about being able to do things like that, or if there is a
> bug somewhere else.  BTW, you'll only get this if you compile with -O.

According to the ISO C Standard, macro invocations cannot contain other
preprocessing directives. Also, any standard library function may be
replaced by a macro that has the same effect. Therefore, you can't
(reliably) put directives in the argument list of any standard library
function.

Workarounds include:

1. Put "#undef strncpy" after "#include <string.h> but before the
strncpy() invocation (this works (as a NOOP) even if strncpy isn't
defined as a macro).

2. Change the strncpy() invocation to "(strncpy) (arglist)"; this
prevents it from being recognized as a macro.

3. Rewrite it so the ifdef is before the invocation (as someone else
suggested), which would be a lot prettier, but a pain in the ass if
there are lots of such invocations.

Steve


Reply to: