fix string off-by-one bugs