Skip to content

Commit 8a0b598

Browse files
committed
Fix integer overflow with max limit & batchsize (#1810)
Eliminate unnecessary killCursors command when batchSize == limit can overflow if using Integer.MAX_VALUE for both. JAVA-5667 JAVA-5970
1 parent 002adfe commit 8a0b598

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

driver-core/src/main/com/mongodb/internal/operation/FindOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private BsonDocument getCommand(final OperationContext operationContext, final i
390390
commandDocument.put("limit", new BsonInt32(Math.abs(batchSize)));
391391
} else if (batchSize != 0) {
392392
int effectiveBatchSize = Math.abs(batchSize);
393-
if (effectiveBatchSize == limit) {
393+
if (effectiveBatchSize == limit && effectiveBatchSize < Integer.MAX_VALUE) {
394394
// avoid an open cursor on server side when batchSize and limit are equal
395395
effectiveBatchSize++;
396396
}

driver-core/src/test/unit/com/mongodb/internal/operation/FindOperationUnitSpecification.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ class FindOperationUnitSpecification extends OperationUnitSpecification {
102102
version << [[3, 2, 0]] * 10 + [[3, 4, 0]] * 10
103103
}
104104

105+
def 'should find with correct command with effective batch size'() {
106+
when:
107+
def operation = new FindOperation<BsonDocument>(namespace, new BsonDocumentCodec())
108+
.batchSize(batchSize)
109+
.limit(limit)
110+
111+
def expectedCommand = new BsonDocument('find', new BsonString(namespace.getCollectionName()))
112+
.append('batchSize', new BsonInt32(commandBatchSize))
113+
.append('limit', new BsonInt32(commandLimit))
114+
115+
then:
116+
testOperation(operation, [7, 0, 0], expectedCommand, async, commandResult)
117+
118+
where:
119+
async << [true, true, false, false]
120+
batchSize << [10, Integer.MAX_VALUE] * 2
121+
limit << [10, Integer.MAX_VALUE] * 2
122+
commandLimit << [10, Integer.MAX_VALUE] * 2
123+
commandBatchSize << [11, Integer.MAX_VALUE] * 2
124+
}
125+
105126
def 'should use the readPreference to set secondaryOk for commands'() {
106127
when:
107128
def operation = new FindOperation<Document>(namespace, new DocumentCodec())

0 commit comments

Comments
 (0)