Skip to content

Commit 8b8b0ae

Browse files
GODRIVER-3240 [v1] Replace all bit-shifting in wiremessage with encoding/binary calls (#1670)
Co-authored-by: Matt Dale <[email protected]>
1 parent 0f10f5e commit 8b8b0ae

File tree

3 files changed

+489
-15
lines changed

3 files changed

+489
-15
lines changed

x/mongo/driver/operation.go

-1
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,6 @@ func (op Operation) decodeResult(ctx context.Context, opcode wiremessage.OpCode,
18931893
return nil, errors.New("malformed wire message: insufficient bytes to read single document")
18941894
}
18951895
case wiremessage.DocumentSequence:
1896-
// TODO(GODRIVER-617): Implement document sequence returns.
18971896
_, _, wm, ok = wiremessage.ReadMsgSectionDocumentSequence(wm)
18981897
if !ok {
18991898
return nil, errors.New("malformed wire message: insufficient bytes to read document sequence")

x/mongo/driver/wiremessage/wiremessage.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package wiremessage
1515

1616
import (
1717
"bytes"
18+
"encoding/binary"
1819
"strings"
1920
"sync/atomic"
2021

@@ -238,10 +239,11 @@ func ReadHeader(src []byte) (length, requestID, responseTo int32, opcode OpCode,
238239
if len(src) < 16 {
239240
return 0, 0, 0, 0, src, false
240241
}
241-
length = (int32(src[0]) | int32(src[1])<<8 | int32(src[2])<<16 | int32(src[3])<<24)
242-
requestID = (int32(src[4]) | int32(src[5])<<8 | int32(src[6])<<16 | int32(src[7])<<24)
243-
responseTo = (int32(src[8]) | int32(src[9])<<8 | int32(src[10])<<16 | int32(src[11])<<24)
244-
opcode = OpCode(int32(src[12]) | int32(src[13])<<8 | int32(src[14])<<16 | int32(src[15])<<24)
242+
243+
length = readi32unsafe(src)
244+
requestID = readi32unsafe(src[4:])
245+
responseTo = readi32unsafe(src[8:])
246+
opcode = OpCode(readi32unsafe(src[12:]))
245247
return length, requestID, responseTo, opcode, src[16:], true
246248
}
247249

@@ -577,12 +579,16 @@ func ReadKillCursorsCursorIDs(src []byte, numIDs int32) (cursorIDs []int64, rem
577579
return cursorIDs, src, true
578580
}
579581

580-
func appendi32(dst []byte, i32 int32) []byte {
581-
return append(dst, byte(i32), byte(i32>>8), byte(i32>>16), byte(i32>>24))
582+
func appendi32(dst []byte, x int32) []byte {
583+
b := []byte{0, 0, 0, 0}
584+
binary.LittleEndian.PutUint32(b, uint32(x))
585+
return append(dst, b...)
582586
}
583587

584-
func appendi64(b []byte, i int64) []byte {
585-
return append(b, byte(i), byte(i>>8), byte(i>>16), byte(i>>24), byte(i>>32), byte(i>>40), byte(i>>48), byte(i>>56))
588+
func appendi64(dst []byte, x int64) []byte {
589+
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
590+
binary.LittleEndian.PutUint64(b, uint64(x))
591+
return append(dst, b...)
586592
}
587593

588594
func appendCString(b []byte, str string) []byte {
@@ -594,21 +600,18 @@ func readi32(src []byte) (int32, []byte, bool) {
594600
if len(src) < 4 {
595601
return 0, src, false
596602
}
597-
598-
return (int32(src[0]) | int32(src[1])<<8 | int32(src[2])<<16 | int32(src[3])<<24), src[4:], true
603+
return readi32unsafe(src), src[4:], true
599604
}
600605

601606
func readi32unsafe(src []byte) int32 {
602-
return (int32(src[0]) | int32(src[1])<<8 | int32(src[2])<<16 | int32(src[3])<<24)
607+
return int32(binary.LittleEndian.Uint32(src))
603608
}
604609

605610
func readi64(src []byte) (int64, []byte, bool) {
606611
if len(src) < 8 {
607612
return 0, src, false
608613
}
609-
i64 := (int64(src[0]) | int64(src[1])<<8 | int64(src[2])<<16 | int64(src[3])<<24 |
610-
int64(src[4])<<32 | int64(src[5])<<40 | int64(src[6])<<48 | int64(src[7])<<56)
611-
return i64, src[8:], true
614+
return int64(binary.LittleEndian.Uint64(src)), src[8:], true
612615
}
613616

614617
func readcstring(src []byte) (string, []byte, bool) {

0 commit comments

Comments
 (0)