Last active
December 11, 2015 14:59
-
-
Save foobit/4618064 to your computer and use it in GitHub Desktop.
64bit (OSX and *nix) vsnprintf trashes va_list args. Must va_start between requesting length and performing expansion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string string_format(const char* s, ...) | |
{ | |
va_list args; | |
va_start(args, s); | |
std::string result; | |
int len = vsnprintf(nullptr, 0, s, args); | |
if (len > 0) | |
{ | |
// va_end/va_start hack to fix *nix 64bit versions inc Mac OS X | |
va_end(args); | |
va_start(args, s); | |
scope_array<char> tmp(len+1); | |
vsnprintf(tmp.get(), tmp.size(), s, args); | |
result = tmp; | |
} | |
va_end(args); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Workaround Source
http://www.linuxquestions.org/questions/programming-9/vsnprintf-is-broken-301332