Fix warnings about unsafe integer casts
Fix potential buffer overflow

 * vsprintf() can overflow the output buffer; use vsnprintf() instead.
 * unnecessary calls to strlen()
   - on every iteration just to see if the string end has been reached yet,
   - even though the upper bound can be derived from the return value of
     vsnprintf().