Samba 2.2.2 problem in lib/sprintf.c
Paul.Green at stratus.com
Tue Nov 27 08:35:02 GMT 2001
Oh, I love arguing about language definitions...sigh...cough. Let's give
this issue a try.
I cannot find any reference in either C Standard (ANSI/ISO 9899:1990 or
ANSI/ISO 9899:1999) that says that if you include a standard header, you get
the standard function it defines. Both versions of the standard say (7.1.7)
that "any function declared in a header may be additionally implemented as a
macro defined in the header, so a library function should not be declared
explicitly if its header is included." and "Provided that a library function
can be declared without reference to any type defined in a header, it is
also permissible to declare the function, either explicitly or implicitly,
and use it without including its associated header. If a function that
accepts a variable number of arguments is not declared (explicitly or by
including its associated header), the behavior is undefined." Not much
The standard explicitly says (7.1.7) that an implementation can replace
functions with macros for any reason, including performing optimizations,
but that the programmer can always remove the macro by performing an #undef
on the name (or by other syntactic tricks).
The closest you get to tying a header to a specific function is a statement
that if a header defines a symbol to have external linkage, then that
identifier is always required to have external linkage (7.1.3). But it
never says the function has to do the same thing.
POSIX.1, unlike C, subtly requires that headers be used to access library
functions (See B.2.7.2, last paragraph). It picks up all of the Standard C
language about how functions can be overloaded by macros of the same name.
John E. Malmberg <wb8tyw at qsl.net> reports that Compaq C performs
optimizations on the snprintf function when it is defined by <stdio.h>. It
seems to me that such behavior is beyond what is specified by Standard C but
acceptable under POSIX.1. You include the standard headers, you get the
standard functions. The compiler is perfectly within its rights to do this;
it could inline 100% of all references to every function defined in a
standard header if it wanted to.
I do think that Compaq C would have been better off implementing its
optimizations using a macro form of snprintf, as then both Standard C and
POSIX.1 provide a simple way to avoid any such macros.
I agree that the workaround for Samba is to have a new rule that when it
wants to invoke its own copy of snprintf it has to do so from a source file
that does not include <stdio.h>. Or, have a macro that lexically renames
"snprintf" to (say) "my_own_snprintf"; this might be an easier solution
because it avoids having to get rid of <stdio.h>.
Hope this helps.
Paul Green, Senior Technical Consultant, Stratus Technologies.
Day: +1 978-461-7557; FAX: +1 978-461-3610
Speaking from Stratus not for Stratus
More information about the samba-technical