summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java7
1 files changed, 7 insertions, 0 deletions
diff --git a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
index 304c042..662f0fa 100644
--- a/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
+++ b/java/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java
@@ -500,6 +500,13 @@ public final class CodedOutputByteBufferNano {
bytes[j++] = (byte) (0x80 | (0x3F & (codePoint >>> 6)));
bytes[j++] = (byte) (0x80 | (0x3F & codePoint));
} else {
+ // If we are surrogates and we're not a surrogate pair, always throw an
+ // IllegalArgumentException instead of an ArrayOutOfBoundsException.
+ if ((Character.MIN_SURROGATE <= c && c <= Character.MAX_SURROGATE)
+ && (i + 1 == sequence.length()
+ || !Character.isSurrogatePair(c, sequence.charAt(i + 1)))) {
+ throw new IllegalArgumentException("Unpaired surrogate at index " + i);
+ }
throw new ArrayIndexOutOfBoundsException("Failed writing " + c + " at index " + j);
}
}