Re: Issue with vasprintf
Posted: Sat Apr 23, 2016 9:15 pm
Thanks again for the advice. I was able to get it working after fiddling with it for a while- using a valid char pointer instread of NULL prevents the craches, but apparently passing in a size of 0 causes issues as well. Using a size of 1 seems to work fine, however. So here's what I finally ended up with:
Code: Select all
int vasprintf(char** strp, const char* fmt, va_list ap) {
va_list ap2;
va_copy(ap2, ap);
char tmp[1];
int size = vsnprintf(tmp, 1, fmt, ap2);
if (size <= 0) return size;
va_end(ap2);
size += 1;
*strp = (char*)malloc(size * sizeof(char));
return vsnprintf(*strp, size, fmt, ap);
}