fix swprintf internal buffer state and error handling