浏览代码

varintLengthField.reserveLen() should return 0 during decode

Vlad Hanciuta 8 年之前
父节点
当前提交
033fed9b92
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      length_field.go

+ 6 - 0
length_field.go

@@ -31,10 +31,12 @@ func (l *lengthField) check(curOffset int, buf []byte) error {
 type varintLengthField struct {
 	startOffset int
 	length      int64
+	consumed    bool
 }
 
 func (l *varintLengthField) decode(pd packetDecoder) error {
 	var err error
+	l.consumed = true
 	l.length, err = pd.getVarint()
 	return err
 }
@@ -51,6 +53,10 @@ func (l *varintLengthField) adjustLength(currOffset int) int {
 }
 
 func (l *varintLengthField) reserveLength() int {
+	if l.consumed { // the field was consumed during the decode
+		l.consumed = false
+		return 0
+	}
 	var tmp [binary.MaxVarintLen64]byte
 	return binary.PutVarint(tmp[:], l.length)
 }