Question

In MSVC, a number of string functions offer the original, a safe version, and a strsafe version. For example, sprintf, sprintf_s, and StringCchPrintf are all equivalents, increasing in safety (by some metric).

Now, I have a bit of code that does:

int bufsize = _vscwprintf(fmt, args) + 1;
std::vector<wchar_t> buffer(bufsize);

int len = _vsnwprintf_s(&buffer[0], bufsize, bufsize-1, fmt, args);

To allocate a buffer of the correct size.

While looking through the strsafe functions, I found an alternative for _vsnwprintf_s, but none for _vscwprintf. A check of Google didn't seem to return anything.

Is there a strsafe way of writing that bit of code, or alternate functions for both that I'm missing, or is mixing an original and strsafe function acceptable? (no safety warning are given about the current way, on /w4 with PREfast all rules)

Was it helpful?

Solution

_vscwprintf() merely computes the size of the wchar_t[] array you need to safely format the string, it doesn't actually write anything to a buffer. Accordingly you don't need and there is no safe version of the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top