Skip to contentSkip to conversation

Building packages, problems with printf

parisc-linux Matthew Wilcox 4 participants 6 messages Mar 25, 2001 – Apr 4, 2001

lore
Share & tools
lore
#0 lore raw

Something we've been running into when building packages is the problem that printf is defined as a macro when using gcc 2.97. This should be resolved by doing the following:

#include <stdio.h>
#undef printf

fixing glibc to not do that seems to not be an option. ANSI explicitly allows any of the functions in libc to also be defined as macros.

Signature · 1 line
Revolutions do not require corporate support.
#1 lore raw
Quoted 6 lines · Matthew Wilcox

Something we've been running into when building packages is the problem that printf is defined as a macro when using gcc 2.97. This should be resolved by doing the following:

#include <stdio.h>
#undef printf

That's cheating! The real fix is to turn

printf (foo_string,
#if SOMETHING
abc_param
#else
xyz_param
#endif
);

into

#if SOMETHING
printf (foo_string, abc_param);
#else
printf (foo_string, xyz_param);
#endif

Earn brownie points sending off proper fixes to package maintainers. :)

Quoted 2 lines

fixing glibc to not do that seems to not be an option. ANSI explicitly allows any of the functions in libc to also be defined as macros.

Alan

Signature · 1 line
Linuxcare
#2 lore raw
Quoted 12 lines · Alan Modra

That's cheating! The real fix is to turn

printf (foo_string,
#if SOMETHING
abc_param
#else
xyz_param
#endif
);

into

Quoted 7 lines

#if SOMETHING
printf (foo_string, abc_param);
#else
printf (foo_string, xyz_param);
#endif

Earn brownie points sending off proper fixes to package maintainers. :)

Ah, but that doesn't cure netkit-telnet which is c++ and has a method called printf which gets royally bollixed by this define.

Agreed that's a better solution though for the cases where it applies.

Signature · 1 line
Revolutions do not require corporate support.
#3 lore raw
In reply to Alan Modra
Quoted 1 line

Earn brownie points sending off proper fixes to package maintainers. :)

Most package maintainers wont bother. printf as a macro breaks too much code to be 'interesting' to fix. Many people take the address of printf for passing to functions using multiple I/O output methods

Quoted 2 lines
Quoted 2 lines

fixing glibc to not do that seems to not be an option. ANSI explicitly allows any of the functions in libc to also be defined as macros.

glibc may be ANSI compliant, its just useless if it does this. Useful and standards compliant interesect but dont 100% overlap

Alan

#4 lore raw
Quoted 5 lines · Alan Cox
Quoted 1 line

Earn brownie points sending off proper fixes to package maintainers. :)

Most package maintainers wont bother. printf as a macro breaks too much code to be 'interesting' to fix. Many people take the address of printf for passing to functions using multiple I/O output methods

you can still do that. it's still an external function, but for `optimisation' purposes calls to it go to fprintf. i think this is a worthless optimisation myself.

perhaps you can convince drepper of this? :-)

Signature · 1 line
Revolutions do not require corporate support.
#5 lore raw
In reply to Alan Modra
Quoted 19 lines · Alan Modra
Quoted 6 lines · Matthew Wilcox

Something we've been running into when building packages is the problem that printf is defined as a macro when using gcc 2.97. This should be resolved by doing the following:

#include <stdio.h>
#undef printf

That's cheating! The real fix is to turn

printf (foo_string,
#if SOMETHING
abc_param
#else
xyz_param
#endif
);

Quoted 7 lines

into

#if SOMETHING
printf (foo_string, abc_param);
#else
printf (foo_string, xyz_param);
#endif

Actually, another solution is:

(printf) (foo_string,
#if SOMETHING
abc_param
#else
xyz_param
#endif
);

The parentheses surrounding printf prevent macro expansion -- the C standard explicitly states that it must. We discovered this in the course of developing FormatGuard, a patch to glibc that turns printf and the like into macros as a means of preventing format string attacks (see http://www.immunix.org/formatguard.html for details).

Signature · 3 lines
Steve Beattie                               Don't trust programmers? 
<[email protected]>                         Complete StackGuard distro at
http://immunix.org/~steve/                         immunix.org