Search code examples
c++formatwchar-t

How to calculate the size needed for formated wchar array, `CRT_SECURE` way?


I need to calculate the size need for formated wchar array.
Consider following code:

wchar_t* Format(const wchar_t* format, ...)
{
    va_list args;
    va_start(args, format);

    wchar_t *w = NULL;
    int len = _vsnwprintf(NULL, 0, format, args) + 1;            // <------ Error
    if (len > 0)
    {
        w = new wchar_t[len];
        w[0] = 0;
        _vsnwprintf_s(w, len, len, format, args);
    }
    va_end(args);

    return w;
}

if _CRT_SECURE_NO_WARNINGS is turned on, above code work, if not, the error say:

'_vsnwprintf': This function or variable may be unsafe. Consider using _vsnwprintf_s instead.

But I can not manage to use _vsnwprintf_s to calculate the size needed for the wchar buffer.

Following is what I tried:

int len = _vsnwprintf_s(NULL, 0, 0, format, args); // return 1

// debug assertion failed: string != NULL && DstSizeInWords > 0
int len = _vsnwprintf_s(NULL, 0, INT_MAX, format, args);

// debug assertion failed: string != NULL && DstSizeInWords > 0
int len = _vsnwprintf_s(NULL, INT_MAX, 0, format, args);

// debug assertion failed: string != NULL && DstSizeInWords > 0
int len = _vsnwprintf_s(NULL, INT_MAX, INT_MAX, format, args);

Other function: _snwprintf give _CRT_SECURE error and _snwprintf_s have same effect with _vsnwprintf_s

Question:

Although I pretty okay with _CRT_SECURE_NO_WARNINGS turned on, I wonder how can I calculate the size needed for formated wchar array, CRT_SECURE way? Note that it must accept va_list as agrument.


Solution

  • You should be able to obtain required buffer size using _vscwprintf:

    _Check_return_ _Ret_z_ wchar_t *
    Format(_Printf_format_string_ _In_z_ wchar_t const * const psz_format, ...)
    {
        size_t formatted_string_length;
        va_list args;
        {
            va_start(args, psz_format);
            int const result(::_vscwprintf(psz_format, args));
            va_end(args);
            if(-1 == result)
            {
                ::abort();
            }
            assert(0 <= result);
            formatted_string_length = static_cast< size_t >(result);
        }
        size_t const buffer_size(formatted_string_length + 1u);
        wchar_t * const p_buffer(new wchar_t[buffer_size]);
        {
            va_start(args, psz_format);
            int const result(::_vsnwprintf_s(p_buffer, buffer_size, formatted_string_length, psz_format, args));
            va_end(args);
            if(-1 == result)
            {
                ::abort();
            }
            assert(0 <= result);
            assert(static_cast< size_t >(result) == formatted_string_length);
            assert(L'\0' == p_buffer[formatted_string_length]);
        }
        return p_buffer;
    }