c++ - Why is vsnprintf safe? -
i have looked @ question these pdfs' 1 , 2, page , pretty understand happens if printf(some_test_string)
. not understand why ensuring size of buffer vsnprintf
becomes safe compared vsprintf
?
what happens in these 2 cases ?
case 1
char buf[3]; vsprint(buf, "%s", args);
case 2
char buf[3]; vsnprint(buf, sizeof buf, "%s", args);
in case 1, if string you're formatting has length of 3 or greater, have buffer overrun, vsprintf might write memory past storage of buf array, undefined behavior, possibly causing havoc/security concerns/crashes/etc.
in case 2. vsnprintf knows how big buffer contain result is, , make sure not go past that(instead truncating result fit within buf
).
Comments
Post a Comment