Skip to content

Commit d677083

Browse files
committed
JAVA-891: Added an additional check for NULL character to BasicBSONEncoder.
1 parent 4ac8e84 commit d677083

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/main/org/bson/BasicBSONEncoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ protected int _put( String str ){
480480
for ( int i=0; i<len; ){
481481
int c = Character.codePointAt( str , i );
482482

483+
if (c == 0x0) {
484+
throw new BSONException(
485+
String.format("BSON cstring '%s' is not valid because it contains a null character at index %d", str, i));
486+
}
487+
483488
if ( c < 0x80 ){
484489
_buf.write( (byte)c );
485490
total += 1;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.bson;
19+
20+
import org.bson.io.BasicOutputBuffer;
21+
import org.testng.Assert;
22+
import org.testng.annotations.Test;
23+
24+
public class BasicBSONEncoderTest extends Assert {
25+
26+
@Test(expectedExceptions = BSONException.class)
27+
public void testNullCharacterInCString() {
28+
BasicBSONEncoder encoder = new BasicBSONEncoder();
29+
encoder.set(new BasicOutputBuffer());
30+
encoder.writeCString("hell\u0000world");
31+
}
32+
}

0 commit comments

Comments
 (0)