[英]vsprintf on OS X: EXC_BAD_ACCESS
I've discovered weird behavior of vsprintf on OSX. 我在OSX上发现了vsprintf的奇怪行为。
This is my code: 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#if defined(WIN32)
#include <windows.h>
#define VSNPRINTF _vsnprintf
#elif defined(LINUX) || defined (DARWIN)
#define VSNPRINTF vsnprintf
#include <sys/types.h>
#include <unistd.h>
#endif
char *f(const char *fmt, ...)
{
char *out = NULL;
const int step = 32;
int n = -1, lout = step;
va_list arg;
if(fmt!=NULL)
{
va_start(arg, fmt);
do
{
if(!out)
{
free(out);
out = NULL;
}
out = (char*)malloc(lout + 1);
if(!out) break;
memset(out, 0, lout + 1);
n = VSNPRINTF(out, lout, fmt, arg);
if(n == -1 || n + 1 > lout)
{
lout += step;
n = -1;
}
}while(n == -1);
va_end(arg);
}
return out;
}
int main()
{
char *msg = NULL;
unsigned long x = 0xDEADBEEF;
msg = f("%X%X%X%X", x, x, x, x);
if(!msg) return -1;
puts(msg);
return 0;
}
The function shall return allocated string (char*) containing formatted text. 该函数应返回包含格式化文本的已分配字符串(char *)。 It works correctly on Linux and Windows.
它在Linux和Windows上正常运行。 It returns badly formatted text on OSX and sometimes it leads to Segmentation fault (EXC_BAD_ACCESS).
它在OSX上返回格式错误的文本,有时会导致分段错误(EXC_BAD_ACCESS)。 Btw, I know that I can use vasprintf.
顺便说一句,我知道我可以使用vasprintf。
What can be the problem? 可能是什么问题?
Your problem is most likely that you're calling vsnprintf with the same va_list multiple times. 您的问题很可能是您多次使用相同的va_list调用vsnprintf。 This doesn't work in some ABIs.
这在某些ABI中不起作用。
Look up the man page of va_copy. 查找va_copy的手册页。 The short version is to do something like this:
简短的版本是做这样的事情:
va_list c;
va_copy(c, arg);
n = VSNPRINTF(out, lout, fmt, c);
va_end(c);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.