vswprintf() without buffer size crashes on small buffer instead of EOF. How to pass buffer size

400 Views Asked by At

Using Borland C++ Builder 2009

I use vswprintf per RAD Studio's Help (F1) :

int vswprintf(wchar_t *buffer, const wchar_t *format, va_list arglist); 

Up to now I always provided a big buffer wchar_t OutputStr[1000] and never had any issues. As a test and wanting to do an improvement action, I tried a small buffer wchar_t OutputStr[12] and noticed that the program crashes entirely. Even try{}catch(...){} doesn't catch it. Codeguard reports that a memcpy() fails, which seems to be the internal implementation. I had expected an EOF as return value instead.

When searching online for vswprintf I find the c++ variant takes a buffer size as input, but I can't seem to convince my compiler to use that variant ? Any idea how to force it using BCB2009 ?

The whole point of the exercise was to implement a fall back scenario for when the buffer is too small in possibly one or two freak situations, so that I can allocate more memory for the function and try again. But this mechanism doesn't seem to work at all.

Not sure how to best test for the exact amount of bytes/characters needed either ?

1

There are 1 best solutions below

8
On

You can use vswprintf_s. It returns negative value on failure