StreamInput throws exceptions instead of using assertions (#24294)
StreamInput has methods such as readVInt that perform sanity checks on the data using assertions, which will catch bad data in tests but provide no safety when running as a node without assertions enabled. The use of assertions also make testing with invalid data difficult since we would need to handle assertion errors in the code using the stream input and errors like this should not be something we try to catch. This commit introduces a flag that will throw an IOException instead of using an assertion.
This commit is contained in:
parent
c17de49a6d
commit
7f8fe8b81d
|
@ -202,7 +202,9 @@ public abstract class StreamInput extends InputStream {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
b = readByte();
|
b = readByte();
|
||||||
assert (b & 0x80) == 0;
|
if ((b & 0x80) != 0) {
|
||||||
|
throw new IOException("Invalid vInt ((" + Integer.toHexString(b) + " & 0x7f) << 28) | " + Integer.toHexString(i));
|
||||||
|
}
|
||||||
return i | ((b & 0x7F) << 28);
|
return i | ((b & 0x7F) << 28);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -367,7 +369,7 @@ public abstract class StreamInput extends InputStream {
|
||||||
buffer[i] = ((char) ((c & 0x0F) << 12 | (readByte() & 0x3F) << 6 | (readByte() & 0x3F) << 0));
|
buffer[i] = ((char) ((c & 0x0F) << 12 | (readByte() & 0x3F) << 6 | (readByte() & 0x3F) << 0));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
new AssertionError("unexpected character: " + c + " hex: " + Integer.toHexString(c));
|
throw new IOException("Invalid string; unexpected character: " + c + " hex: " + Integer.toHexString(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return spare.toString();
|
return spare.toString();
|
||||||
|
@ -808,7 +810,7 @@ public abstract class StreamInput extends InputStream {
|
||||||
case 17:
|
case 17:
|
||||||
return (T) readStackTrace(new IOException(readOptionalString(), readException()), this);
|
return (T) readStackTrace(new IOException(readOptionalString(), readException()), this);
|
||||||
default:
|
default:
|
||||||
assert false : "no such exception for id: " + key;
|
throw new IOException("no such exception for id: " + key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in New Issue