From 7f8fe8b81db5f90f5449638ba8a817f9d3dd5918 Mon Sep 17 00:00:00 2001 From: Jay Modi Date: Wed, 26 Apr 2017 07:23:07 -0400 Subject: [PATCH] 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. --- .../org/elasticsearch/common/io/stream/StreamInput.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index e33c3ed840a..7d175916c8e 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -202,7 +202,9 @@ public abstract class StreamInput extends InputStream { return i; } 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); } @@ -367,7 +369,7 @@ public abstract class StreamInput extends InputStream { buffer[i] = ((char) ((c & 0x0F) << 12 | (readByte() & 0x3F) << 6 | (readByte() & 0x3F) << 0)); break; 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(); @@ -808,7 +810,7 @@ public abstract class StreamInput extends InputStream { case 17: return (T) readStackTrace(new IOException(readOptionalString(), readException()), this); default: - assert false : "no such exception for id: " + key; + throw new IOException("no such exception for id: " + key); } } return null;