Browse Source

fix possible name overflow when len(name)>255

pull/800/head
Chris Lu 6 years ago
parent
commit
8f78a069c6
  1. 10
      weed/storage/needle_read_write.go

10
weed/storage/needle_read_write.go

@ -9,6 +9,7 @@ import (
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
. "github.com/chrislusf/seaweedfs/weed/storage/types" . "github.com/chrislusf/seaweedfs/weed/storage/types"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"math"
) )
const ( const (
@ -65,7 +66,12 @@ func (n *Needle) Append(w io.Writer, version Version) (size uint32, actualSize i
header := make([]byte, NeedleEntrySize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation header := make([]byte, NeedleEntrySize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation
CookieToBytes(header[0:CookieSize], n.Cookie) CookieToBytes(header[0:CookieSize], n.Cookie)
NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id) NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
n.DataSize, n.NameSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Name)), uint8(len(n.Mime))
if len(n.Name) >= math.MaxUint8 {
n.NameSize = math.MaxInt8
} else {
n.NameSize = uint8(len(n.Name))
}
n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime))
if n.DataSize > 0 { if n.DataSize > 0 {
n.Size = 4 + n.DataSize + 1 n.Size = 4 + n.DataSize + 1
if n.HasName() { if n.HasName() {
@ -108,7 +114,7 @@ func (n *Needle) Append(w io.Writer, version Version) (size uint32, actualSize i
if _, err = w.Write(header[0:1]); err != nil { if _, err = w.Write(header[0:1]); err != nil {
return return
} }
if _, err = w.Write(n.Name); err != nil {
if _, err = w.Write(n.Name[:n.NameSize]); err != nil {
return return
} }
} }

Loading…
Cancel
Save