svn commit: trunk/busybox/util-linux

Denis Vlasenko vda.linux at googlemail.com
Thu Sep 21 11:04:29 UTC 2006


On Wednesday 20 September 2006 19:25, Rob Landley wrote:
> Where did "linenumber" come from in the first place?  In the macro you're 
> trying to use.  If you declare it as a local variable, and you've #defined it 
> to 0, it throws an error.  If it's one of the macro arguments, then that 
> takes precedence in the macro scope over external redefinitions.
> 
> #include <stdio.h>
> 
> #define thingy(one,two) printf(one,two);
> #define two walrus
> 
> int main(int argc, char *argv)
> {
>   thingy("hello %s\n","world");
> }
> 
> gcc hello.c
> ./a.out
> hello world
> 
> Can you come up with a real example of this causing an actual problem?  Give 
> me something I can compile, like the above.  (P.S.  Use ALL_CAPS for your 
> #define constants, as we do in the real world.)

In mount.c we use #define useMtab 0 because we need to match
variable name.

Ok, I put together an example as requested.
external_header.h is outside of applet.c author control,
it's supposed to be from a library.

# cat external_header.h
#include <stdio.h>

extern int last_line;
extern int linenumber;

#define pchar(c) \
        ({ \
        char cc = (c); \
        if (cc!='\n') { last_line = linenumber; } \
        putchar(cc); \
        })

# cat applet.c
#include <stdio.h>
#include <external_header.h>

#ifdef CONFIG_FOO
int linenumber;
#else
#define linenumber 0
#endif

int main()
{
        pchar('H');
        pchar('e');
        pchar('l');
        pchar('l');
        pchar('o');
        return 0;
}

I compiled applet.c to applet.s and I see there stores
of zero to last_line instead of stores of linenumber.

I am not claiming that this code is sane. It is not.

I merely say that using enum here would catch the error
whereas #define silently produces wrong code.

With enum:

# gcc -I . applet.c -S
applet.c:7: error: 'linenumber' redeclared as different kind of symbol
./external_header.h:4: error: previous declaration of 'linenumber' was here
applet.c:7: error: 'linenumber' redeclared as different kind of symbol
./external_header.h:4: error: previous declaration of 'linenumber' was here

--
vda



More information about the busybox mailing list