Skip to content

Commit ffcdfe0

Browse files
committed
pr feedback
1 parent ad9e2aa commit ffcdfe0

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/bson.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ export const readInt32LE = (buffer: Uint8Array, offset: number): number => {
5555
return NumberUtils.getInt32LE(buffer, offset);
5656
};
5757

58+
export const setUint32LE = (destination: Uint8Array, offset: number, value: number): 4 => {
59+
destination[offset] = value;
60+
value >>>= 8;
61+
destination[offset + 1] = value;
62+
value >>>= 8;
63+
destination[offset + 2] = value;
64+
value >>>= 8;
65+
destination[offset + 3] = value;
66+
return 4;
67+
};
68+
5869
/**
5970
* BSON Serialization options.
6071
* @public

src/cmap/commands.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
type Document,
66
type Long,
77
NumberUtils,
8-
readInt32LE
8+
readInt32LE,
9+
setUint32LE
910
} from '../bson';
1011
import { MongoInvalidArgumentError, MongoRuntimeError } from '../error';
1112
import { type ReadPreference } from '../read_preference';
@@ -577,8 +578,7 @@ export class OpMsgRequest {
577578
NumberUtils.setInt32LE(header, 12, OP_MSG); // opCode
578579
// The OP_MSG spec calls out that flags is uint32:
579580
// https://github.com/mongodb/specifications/blob/master/source/message/OP_MSG.md#op_msg-1
580-
// Flags are limited to only 16 bits, so it does not matter that we are using setInt32LE method.
581-
NumberUtils.setInt32LE(header, 16, flags); // flags
581+
setUint32LE(header, 16, flags); // flags
582582
return buffers;
583583
}
584584

@@ -719,7 +719,8 @@ export class OpMsgResponse {
719719
const payloadType = this.data[this.index++];
720720
if (payloadType === 0) {
721721
// BSON spec specifies that this is a 32-bit signed integer: https://bsonspec.org/spec.html#:~:text=%3A%3A%3D-,int32,-e_list%20unsigned_byte(0
722-
// Max BSON size is 16MB, which is well below 32-bit signed or unsigned limits, so we are choosing to read the value as Uint32
722+
// While allowing negative sizes seems odd, in practice we never expect a negative size. Also, the server's 16mb limit for BSON documents leaves plenty
723+
// of room in an int32 to store a document of the max BSON size that the server supports
723724
const bsonSize = readInt32LE(this.data, this.index);
724725
const bin = this.data.subarray(this.index, this.index + bsonSize);
725726

0 commit comments

Comments
 (0)