Skip to content

Commit 9a26062

Browse files
committed
JAVA-2362: Throw exception if a one-byte BSON string is not properly null-terminated
1 parent 3606a33 commit 9a26062

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

bson/src/main/org/bson/io/ByteBufferBsonInput.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ public String readCString() {
142142
private String readString(final int size) {
143143
if (size == 2) {
144144
byte asciiByte = readByte(); // if only one byte in the string, it must be ascii.
145-
readByte(); // read null terminator
145+
byte nullByte = readByte(); // read null terminator
146+
if (nullByte != 0) {
147+
throw new BsonSerializationException("Found a BSON string that is not null-terminated");
148+
}
146149
if (asciiByte < 0) {
147150
return UTF8_CHARSET.newDecoder().replacement();
148151
}

bson/src/test/unit/org/bson/io/ByteBufferBsonInputSpecification.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,15 @@ class ByteBufferBsonInputSpecification extends Specification {
396396
then:
397397
thrown(BsonSerializationException)
398398
}
399+
400+
def 'should throw BsonSerializationException if a one-byte BSON string is not null-terminated'() {
401+
given:
402+
def stream = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap([2, 0, 0, 0, 1, 3] as byte[])))
403+
404+
when:
405+
stream.readString()
406+
407+
then:
408+
thrown(BsonSerializationException)
409+
}
399410
}

0 commit comments

Comments
 (0)