Skip to content

Commit 87ae788

Browse files
GODRIVER-3303 Remove redundant code; Add test cases. (#1753)
Co-authored-by: Kobrin Ilay <[email protected]>
1 parent 2e7cb37 commit 87ae788

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

mongo/integration/mtest/wiremessage_helpers.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,11 @@ func parseOpCompressed(wm []byte) (wiremessage.OpCode, []byte, error) {
4949
return originalOpcode, nil, errors.New("failed to read uncompressed size")
5050
}
5151

52-
compressorID, wm, ok := wiremessage.ReadCompressedCompressorID(wm)
52+
compressorID, compressedMsg, ok := wiremessage.ReadCompressedCompressorID(wm)
5353
if !ok {
5454
return originalOpcode, nil, errors.New("failed to read compressor ID")
5555
}
5656

57-
compressedMsg, _, ok := wiremessage.ReadCompressedCompressedMessage(wm, int32(len(wm)))
58-
if !ok {
59-
return originalOpcode, nil, errors.New("failed to read compressed message")
60-
}
61-
6257
opts := driver.CompressionOpts{
6358
Compressor: compressorID,
6459
UncompressedSize: uncompressedSize,

x/mongo/driver/operation.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,18 +1199,12 @@ func (Operation) decompressWireMessage(wm []byte) (wiremessage.OpCode, []byte, e
11991199
if !ok {
12001200
return 0, nil, errors.New("malformed OP_COMPRESSED: missing compressor ID")
12011201
}
1202-
compressedSize := len(wm) - 9 // original opcode (4) + uncompressed size (4) + compressor ID (1)
1203-
// return the original wiremessage
1204-
msg, _, ok := wiremessage.ReadCompressedCompressedMessage(rem, int32(compressedSize))
1205-
if !ok {
1206-
return 0, nil, errors.New("malformed OP_COMPRESSED: insufficient bytes for compressed wiremessage")
1207-
}
12081202

12091203
opts := CompressionOpts{
12101204
Compressor: compressorID,
12111205
UncompressedSize: uncompressedSize,
12121206
}
1213-
uncompressed, err := DecompressPayload(msg, opts)
1207+
uncompressed, err := DecompressPayload(rem, opts)
12141208
if err != nil {
12151209
return 0, nil, err
12161210
}

x/mongo/driver/wiremessage/wiremessage.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,9 @@ func ReadMsgSectionSingleDocument(src []byte) (doc bsoncore.Document, rem []byte
381381
// ReadMsgSectionDocumentSequence reads an identifier and document sequence from src and returns the document sequence
382382
// data parsed into a slice of BSON documents.
383383
func ReadMsgSectionDocumentSequence(src []byte) (identifier string, docs []bsoncore.Document, rem []byte, ok bool) {
384-
length, rem, ok := readi32(src)
385-
if !ok || int(length) > len(src) || length-4 < 0 {
386-
return "", nil, rem, false
387-
}
388-
389-
rem, ret := rem[:length-4], rem[length-4:] // reslice so we can just iterate a loop later
390-
391-
identifier, rem, ok = readcstring(rem)
384+
identifier, rem, ret, ok := ReadMsgSectionRawDocumentSequence(src)
392385
if !ok {
393-
return "", nil, rem, false
386+
return "", nil, src, false
394387
}
395388

396389
docs = make([]bsoncore.Document, 0)
@@ -403,7 +396,7 @@ func ReadMsgSectionDocumentSequence(src []byte) (identifier string, docs []bsonc
403396
docs = append(docs, doc)
404397
}
405398
if len(rem) > 0 {
406-
return "", nil, append(rem, ret...), false
399+
return "", nil, src, false
407400
}
408401

409402
return identifier, docs, ret, true
@@ -414,7 +407,7 @@ func ReadMsgSectionDocumentSequence(src []byte) (identifier string, docs []bsonc
414407
func ReadMsgSectionRawDocumentSequence(src []byte) (identifier string, data []byte, rem []byte, ok bool) {
415408
length, rem, ok := readi32(src)
416409
if !ok || int(length) > len(src) || length-4 < 0 {
417-
return "", nil, rem, false
410+
return "", nil, src, false
418411
}
419412

420413
// After these assignments, rem will be the data containing the identifier string + the document sequence bytes and
@@ -423,7 +416,7 @@ func ReadMsgSectionRawDocumentSequence(src []byte) (identifier string, data []by
423416

424417
identifier, rem, ok = readcstring(rem)
425418
if !ok {
426-
return "", nil, rem, false
419+
return "", nil, src, false
427420
}
428421

429422
return identifier, rem, rest, true
@@ -546,14 +539,6 @@ func ReadCompressedCompressorID(src []byte) (id CompressorID, rem []byte, ok boo
546539
return CompressorID(src[0]), src[1:], true
547540
}
548541

549-
// ReadCompressedCompressedMessage reads the compressed wiremessage to dst.
550-
func ReadCompressedCompressedMessage(src []byte, length int32) (msg []byte, rem []byte, ok bool) {
551-
if len(src) < int(length) || length < 0 {
552-
return nil, src, false
553-
}
554-
return src[:length], src[length:], true
555-
}
556-
557542
// ReadKillCursorsZero reads the zero field from src.
558543
func ReadKillCursorsZero(src []byte) (zero int32, rem []byte, ok bool) {
559544
return readi32(src)

x/mongo/driver/wiremessage/wiremessage_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ func TestReadMsgSectionDocumentSequence(t *testing.T) {
175175
wantRem: []byte{0, 1},
176176
wantOK: false,
177177
},
178+
{
179+
desc: "incorrect size",
180+
src: []byte{3, 0, 0},
181+
wantIdentifier: "",
182+
wantDocs: nil,
183+
wantRem: []byte{3, 0, 0},
184+
wantOK: false,
185+
},
186+
{
187+
desc: "insufficient size",
188+
src: []byte{4, 0, 0},
189+
wantIdentifier: "",
190+
wantDocs: nil,
191+
wantRem: []byte{4, 0, 0},
192+
wantOK: false,
193+
},
178194
{
179195
desc: "nil",
180196
src: nil,

0 commit comments

Comments
 (0)