Fix #1038 by catching invalid cursor ranges