Heyas.
Like many others, the Nehe-tutorials use va_list like the following (taken from lesson, hmm, whatever :) )
GLvoid glPrint(const char *fmt, ...)
{
char text[256];
va_list ap;
if (fmt == NULL)
return;
va_start(ap, fmt);
vsprintf(text, fmt, ap);
va_end(ap);
[...]
}
This is a bad way of using a va_list, because as soon as the output string is bigger than 256 chars, we run into a buffer overflow.
Fortunatly there is an easy way to precalculate the size of the output-sting, _vscprintf(). This can be used like so:
GLvoid glPrint(const char *fmt, ...)
{
char* text;
va_list ap;
if (fmt == NULL)
return;
va_start(ap, fmt);
int len = _vscprintf( fmt, ap) + 1;
text = new char[len];
vsprintf(text, fmt, ap);
va_end(ap);
[...]
delete[] text;
}
No more buffer overflow cause text is always as big as needed. It is important to add "+ 1" to the len, because _vscprintf does not count the terminating 0-character. Of course you should not forget to delete text after using it :)
I hope this is found helpfull.
schue