如果格式字符串中无百分号引用,vsprintf()确保va_list不被访问吗?
- 内容介绍
- 文章标签
- 相关推荐
本文共计473个文字,预计阅读时间需要2分钟。
如果将格式化字符串传递给vsprintf()函数,其格式字符串(及其变量)不应包含以下字符:-replacements。是否可以保证不访问va_list参数?换句话说,是:
如果传递给vsprintf()的格式字符串(及其变体)不包含%-references,是否可以保证不访问va_list参数?换句话说,是:
#include <stdarg.h> #include <stdio.h> int main ( void ) { char str[16]; va_list ap; /* never initialized */ (void)vsnprintf(str, sizeof(str), "", ap); return 0; }
符合标准的计划?或者那里有不确定的行为吗?
上面的例子显然是愚蠢的,但想象一下可以通过可变参数函数和fixed-args函数调用的函数,大致简化为:
void somefuncVA ( const char * fmt, va_list ap ) { char str[16]; int n; n = vsnprintf(str, sizeof(str), fmt, ap); /* potentially do something with str */ } void vfoo ( const char * fmt, ... ) { va_list ap; va_start(fmt, ap); somefuncVA(fmt, ap); } void foo ( void ) { va_list ap; /* no way to initialize this */ somefuncVA("", ap); }
int vsprintf(char * restrict s, const char * restrict format, va_list arg);
If the format string passed to
vsprintf()… contains no %-references, is it guaranteed that theva_listargument is not accessed.
没有.
The
vsprintffunction is equivalent tosprintf, with the variable argument list
replaced byarg, which shall have been initialized by theva_startmacro …..
C11dr §7.21.6.13
由于以下代码不符合规范,因此结果是未定义的行为(UB).没有保证. @Eugene Sh.
va_list ap; // vv-- ap not initialized (void)vsnprintf(str, sizeof(str), "", ap);
Is
vsprintf()guaranteed not to accessva_listif format string makes no % references?
通过正确传递的va_list arg,vsprintf()就像sprintf()一样.像下面的代码是可以的.允许传递额外的参数.通过vsprintf(),不访问它们(额外参数),但可以访问va_list arg.
sprintf(buf, "format without percent", 1.2345, 456)`
本文共计473个文字,预计阅读时间需要2分钟。
如果将格式化字符串传递给vsprintf()函数,其格式字符串(及其变量)不应包含以下字符:-replacements。是否可以保证不访问va_list参数?换句话说,是:
如果传递给vsprintf()的格式字符串(及其变体)不包含%-references,是否可以保证不访问va_list参数?换句话说,是:
#include <stdarg.h> #include <stdio.h> int main ( void ) { char str[16]; va_list ap; /* never initialized */ (void)vsnprintf(str, sizeof(str), "", ap); return 0; }
符合标准的计划?或者那里有不确定的行为吗?
上面的例子显然是愚蠢的,但想象一下可以通过可变参数函数和fixed-args函数调用的函数,大致简化为:
void somefuncVA ( const char * fmt, va_list ap ) { char str[16]; int n; n = vsnprintf(str, sizeof(str), fmt, ap); /* potentially do something with str */ } void vfoo ( const char * fmt, ... ) { va_list ap; va_start(fmt, ap); somefuncVA(fmt, ap); } void foo ( void ) { va_list ap; /* no way to initialize this */ somefuncVA("", ap); }
int vsprintf(char * restrict s, const char * restrict format, va_list arg);
If the format string passed to
vsprintf()… contains no %-references, is it guaranteed that theva_listargument is not accessed.
没有.
The
vsprintffunction is equivalent tosprintf, with the variable argument list
replaced byarg, which shall have been initialized by theva_startmacro …..
C11dr §7.21.6.13
由于以下代码不符合规范,因此结果是未定义的行为(UB).没有保证. @Eugene Sh.
va_list ap; // vv-- ap not initialized (void)vsnprintf(str, sizeof(str), "", ap);
Is
vsprintf()guaranteed not to accessva_listif format string makes no % references?
通过正确传递的va_list arg,vsprintf()就像sprintf()一样.像下面的代码是可以的.允许传递额外的参数.通过vsprintf(),不访问它们(额外参数),但可以访问va_list arg.
sprintf(buf, "format without percent", 1.2345, 456)`

