add flexible buffer length checking