fix length checking

fix https://github.com/chrislusf/seaweedfs/issues/890
This commit is contained in:
Chris Lu 2019-03-19 20:56:27 -07:00
parent da871896c3
commit cea5c53bf7

View file

@ -206,15 +206,13 @@ func (n *Needle) ParseNeedleHeader(bytes []byte) {
n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleEntrySize]) n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleEntrySize])
} }
var ErrIndexOutOfRange = fmt.Errorf("index out of range")
func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) { func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
index, lenBytes := 0, len(bytes) index, lenBytes := 0, len(bytes)
if index < lenBytes { if index < lenBytes {
n.DataSize = util.BytesToUint32(bytes[index : index+4]) n.DataSize = util.BytesToUint32(bytes[index : index+4])
index = index + 4 index = index + 4
if int(n.DataSize)+index >= lenBytes { if int(n.DataSize)+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 1)
} }
n.Data = bytes[index : index+int(n.DataSize)] n.Data = bytes[index : index+int(n.DataSize)]
index = index + int(n.DataSize) index = index + int(n.DataSize)
@ -224,8 +222,8 @@ func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
if index < lenBytes && n.HasName() { if index < lenBytes && n.HasName() {
n.NameSize = uint8(bytes[index]) n.NameSize = uint8(bytes[index])
index = index + 1 index = index + 1
if int(n.NameSize)+index >= lenBytes { if int(n.NameSize)+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 2)
} }
n.Name = bytes[index : index+int(n.NameSize)] n.Name = bytes[index : index+int(n.NameSize)]
index = index + int(n.NameSize) index = index + int(n.NameSize)
@ -233,34 +231,34 @@ func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
if index < lenBytes && n.HasMime() { if index < lenBytes && n.HasMime() {
n.MimeSize = uint8(bytes[index]) n.MimeSize = uint8(bytes[index])
index = index + 1 index = index + 1
if int(n.MimeSize)+index >= lenBytes { if int(n.MimeSize)+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 3)
} }
n.Mime = bytes[index : index+int(n.MimeSize)] n.Mime = bytes[index : index+int(n.MimeSize)]
index = index + int(n.MimeSize) index = index + int(n.MimeSize)
} }
if index < lenBytes && n.HasLastModifiedDate() { if index < lenBytes && n.HasLastModifiedDate() {
if LastModifiedBytesLength+index >= lenBytes { if LastModifiedBytesLength+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 4)
} }
n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength]) n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
index = index + LastModifiedBytesLength index = index + LastModifiedBytesLength
} }
if index < lenBytes && n.HasTtl() { if index < lenBytes && n.HasTtl() {
if TtlBytesLength+index >= lenBytes { if TtlBytesLength+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 5)
} }
n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength]) n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
index = index + TtlBytesLength index = index + TtlBytesLength
} }
if index < lenBytes && n.HasPairs() { if index < lenBytes && n.HasPairs() {
if 2+index >= lenBytes { if 2+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 6)
} }
n.PairsSize = util.BytesToUint16(bytes[index : index+2]) n.PairsSize = util.BytesToUint16(bytes[index : index+2])
index += 2 index += 2
if int(n.PairsSize)+index >= lenBytes { if int(n.PairsSize)+index > lenBytes {
return ErrIndexOutOfRange return fmt.Errorf("index out of range %d", 7)
} }
end := index + int(n.PairsSize) end := index + int(n.PairsSize)
n.Pairs = bytes[index:end] n.Pairs = bytes[index:end]