Re: printf doubt
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: 14 Jul 2006 09:00:57 +0200
Keith Thompson wrote:
Joe Wright <joewwright@xxxxxxxxxxx> writes:Ben Pfaff wrote:Barry Schwarz <schwarzb@xxxxxxxxx> writes:Missing the point maybe. There are some number of arguments to printf,
On Tue, 11 Jul 2006 10:09:17 -0700, Ben Pfaff <blp@xxxxxxxxxxxxxxx>What systems use separate stacks for return addresses and
wrote:
Barry Schwarz <schwarzb@xxxxxxxxx> writes:I wonder how systems which push return addresses and arguments on the
Does not the presence of a superfluous argument lead to undefinedNo. From C99 7.19.6.1 "The fprintf function":
behavior?
If the format is exhausted while arguments remain, the
excess arguments are evaluated (as always) but are otherwise
ignored.
same stack, like the old 6502, manage this.
arguments? I am not aware of any in the modern world, so it
would be educational to hear about them. Such a system might be
more resistant to "stack smashing" buffer overflow attacks, for
one thing.
Obviously, fprintf will not pop the superfluous arguments offA common convention is for the callee to pop off the return
the stack since it doesn't even know about them.
address and leave the arguments on the stack. The caller then
pops the arguments.
one of them being the format string. Let's assume a format string and
four other expressions. That's five arguments. If the format string
describes what to do with only two expressions, so be it. Only two
will be treated. But five arguments were pushed and five will be
popped.
Which means that a calling convention where the callee needs to know
how many arguments were passed won't work for printf() or other
variadic functions (unless there's an extra implicit argument that
provides that information).
A C compiler *could* use such a convention for non-variadic functions,
but I think most compilers use the same convention for both (since
there was no such distinction in early C).
At least one popular compiler does not. In the documentation for Microsoft Visual Studio .NET under the description for __stdcall it says:
The __stdcall calling convention is used to call Win32 API functions.
The callee cleans the stack, so the compiler makes vararg functions
__cdecl. Functions that use this calling convention require a
function prototype.
So if the compiler is set to use __stdcall by default it will use different calling conventions for variadic and non-variadic functions.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.
- Follow-Ups:
- Re: printf doubt
- From: Al Balmer
- Re: printf doubt
- References:
- printf doubt
- From: dutche
- Re: printf doubt
- From: Richard Heathfield
- Re: printf doubt
- From: Barry Schwarz
- Re: printf doubt
- From: Ben Pfaff
- Re: printf doubt
- From: Barry Schwarz
- Re: printf doubt
- From: Ben Pfaff
- Re: printf doubt
- From: Joe Wright
- Re: printf doubt
- From: Keith Thompson
- printf doubt
- Prev by Date: Re: [OT] Debugging and Logging
- Next by Date: Re: My gets_ws function
- Previous by thread: Re: printf doubt
- Next by thread: Re: printf doubt
- Index(es):
Relevant Pages
|