fix incorrect boundary check for invalid codepoints