fixed a crash

make sure only rewrite valid locations in the input