简体   繁体   中英

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

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 ?

You can use vswprintf_s . It returns negative value on failure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM