From cea5c53bf7943f7735d74c1e9f568ba633474e5d Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 19 Mar 2019 20:56:27 -0700 Subject: [PATCH] fix length checking fix https://github.com/chrislusf/seaweedfs/issues/890 --- weed/storage/needle_read_write.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/weed/storage/needle_read_write.go b/weed/storage/needle_read_write.go index 4e6bd9cc7..0a73b2977 100644 --- a/weed/storage/needle_read_write.go +++ b/weed/storage/needle_read_write.go @@ -206,15 +206,13 @@ func (n *Needle) ParseNeedleHeader(bytes []byte) { n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleEntrySize]) } -var ErrIndexOutOfRange = fmt.Errorf("index out of range") - func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) { index, lenBytes := 0, len(bytes) if index < lenBytes { n.DataSize = util.BytesToUint32(bytes[index : index+4]) index = index + 4 - if int(n.DataSize)+index >= lenBytes { - return ErrIndexOutOfRange + if int(n.DataSize)+index > lenBytes { + return fmt.Errorf("index out of range %d", 1) } n.Data = bytes[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() { n.NameSize = uint8(bytes[index]) index = index + 1 - if int(n.NameSize)+index >= lenBytes { - return ErrIndexOutOfRange + if int(n.NameSize)+index > lenBytes { + return fmt.Errorf("index out of range %d", 2) } n.Name = bytes[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() { n.MimeSize = uint8(bytes[index]) index = index + 1 - if int(n.MimeSize)+index >= lenBytes { - return ErrIndexOutOfRange + if int(n.MimeSize)+index > lenBytes { + return fmt.Errorf("index out of range %d", 3) } n.Mime = bytes[index : index+int(n.MimeSize)] index = index + int(n.MimeSize) } if index < lenBytes && n.HasLastModifiedDate() { - if LastModifiedBytesLength+index >= lenBytes { - return ErrIndexOutOfRange + if LastModifiedBytesLength+index > lenBytes { + return fmt.Errorf("index out of range %d", 4) } n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength]) index = index + LastModifiedBytesLength } if index < lenBytes && n.HasTtl() { - if TtlBytesLength+index >= lenBytes { - return ErrIndexOutOfRange + if TtlBytesLength+index > lenBytes { + return fmt.Errorf("index out of range %d", 5) } n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength]) index = index + TtlBytesLength } if index < lenBytes && n.HasPairs() { - if 2+index >= lenBytes { - return ErrIndexOutOfRange + if 2+index > lenBytes { + return fmt.Errorf("index out of range %d", 6) } n.PairsSize = util.BytesToUint16(bytes[index : index+2]) index += 2 - if int(n.PairsSize)+index >= lenBytes { - return ErrIndexOutOfRange + if int(n.PairsSize)+index > lenBytes { + return fmt.Errorf("index out of range %d", 7) } end := index + int(n.PairsSize) n.Pairs = bytes[index:end]