I’m attempting to avoid using fixed buffer sizes with things like sprintf and friends (security reasons), however, when I change it to use a sizeof for arg2 -> arg1, my programs text output is corrupted / doesn’t display correctly / missing certain parts.
Specifically, ie:
vsnprintf(putbuf, LARGE_BIG_BUFFER_SIZE, format, args);
to
vsnprintf(putbuf, sizeof putbuf, format, args);
My text output is all corrupted / short with the simple sizeof change. Am I missing something?
original function:
to_screen(const char *format,...)
{
if (window_display && format) {
va_list args;
va_start(args, format);
vsnprintf(putbuf, LARGE_BIG_BUFFER_SIZE, format, args);
va_end(args);
}
}
Put in your code somewhere:
If it’s a pointer, you’ll probably get four or eight since that’ll be the size of a pointer on your system (four or eight will be common ones at the moment but it all depends on the size of your pointers).
Remember that arrays will decay into pointers in the vast majority of cases.
For example:
output this on my system:
If you want to pass the actual size information, you need to do it explicitly:
or, alternatively, for allocated memory: