传递的参数太多的printf [英] Passing too many arguments to printf

查看:1183
本文介绍了传递的参数太多的printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁是已经工作了一个多星期的任何C程序员遇到来自调用导致崩溃的printf 与更多的格式说明不是实际的参数,例如:

Any C programmer who's been working for more than a week has encountered crashes that result from calling printf with more format specifiers than actual arguments, e.g.:

printf("Gonna %s and %s, %s!", "crash", "burn");

不过,有没有类似的不好的事情会发生,当你通过的太多的参数给printf?

However, are there any similar bad things that can happen when you pass too many arguments to printf?

printf("Gonna %s and %s!", "crash", "burn", "dude");

我在x86 / x64汇编知识使我相信,这是无害的,虽然我不相信,有没有我缺少一些边缘状态,我不知道其他架构的想法。这是保证的条件是无害的,或者是有一个潜在的崩溃诱导陷阱也在这里?

My knowledge of x86/x64 assembly leads me to believe that this is harmless, though I'm not convinced that there's not some edge condition I'm missing, and I have no idea about other architectures. Is this condition guaranteed to be harmless, or is there a potentially crash-inducing pitfall here, too?

推荐答案

您可能知道的原型printf函数为这样的事情

You probably know the prototype for the printf function as something like this

int printf(const char *format, ...);

这方面的一个更完整的版本实际上是

A more complete version of that would actually be

int __cdecl printf(const char *format, ...);

__ CDECL 定义了调用约定,它与其它顺水推舟,介绍了参数的处理方式。在这种情况下,它意味着ARGS被推入堆栈,该堆栈通过使该呼叫的功能清洗

The __cdecl defines the "calling convention" which, along with other things, describes how arguments are handled. In the this case it means that args are pushed onto the stack and that the stack is cleaned by the function making the call.

一个替代 _cdecl __ STDCALL ,还有其他的。随着 __ stcall 的约定是参数压入堆栈,并通过调用的函数清理。不过,据我所知,这是不可能的 __ STDCALL 函数接受可变数量的参数。这是有道理的,因为它不知道多少堆栈清理。

One alternative to _cdecl is __stdcall, there are others. With __stcall the convention is that arguments are pushed onto the stack and cleaned by the function that is called. However, as far as I know, it isn't possible for a __stdcall function to accept a variable number of arguments. That makes sense since it wouldn't know how much stack to clean.

长期和短期的它是在的情况下, __ CDECL 功能其安全地传递你想然而,许多ARGS,因为清理在$下进行$ C makeing呼叫。如果你有太多莫名其妙的参数传递给 __ STDCALL 功能,它导致栈的腐败。在那里这可能发生的一个例子是,如果你有错误的雏形。

The long and the short of it is that in the case of __cdecl functions its safe to pass however many args you want, since the cleanup is performed in the code makeing the call. If you were to somehow pass too many arguments to a __stdcall function it result in a corruption of the stack. One example of where this could happen is if you had the wrong prototype.

在调用约定的更多信息,可以发现这里

More information on calling conventions can be found here

这篇关于传递的参数太多的printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆