Add error check for snprintf and vsnprintf