Fix: gRPC multi accepting empty bytes (#411)

This commit is contained in:
Jim Han 2021-03-21 17:16:52 +08:00 committed by GitHub
parent b0e7ad9663
commit 0470381fe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 11 deletions

View File

@ -38,7 +38,7 @@ func NewExisted(b []byte) *Buffer {
oLen := len(b) oLen := len(b)
if oLen < Size { if oLen < Size {
b = append(b, make([]byte, Size-oLen)...) b = b[:Size]
} }
return &Buffer{ return &Buffer{

View File

@ -100,7 +100,7 @@ func (h *HunkReaderWriter) ReadMultiBuffer() (buf.MultiBuffer, error) {
} }
} }
if cap(h.buf) == buf.Size { if cap(h.buf) >= buf.Size {
b := h.buf b := h.buf
h.index = len(h.buf) h.index = len(h.buf)
return buf.MultiBuffer{buf.NewExisted(b)}, nil return buf.MultiBuffer{buf.NewExisted(b)}, nil

View File

@ -79,17 +79,21 @@ func (h *MultiHunkReaderWriter) ReadMultiBuffer() (buf.MultiBuffer, error) {
var mb = make(buf.MultiBuffer, 0, len(h.buf)) var mb = make(buf.MultiBuffer, 0, len(h.buf))
for _, b := range h.buf { for _, b := range h.buf {
if cap(b) >= buf.Size { if len(b) == 0 {
mb = append(mb, buf.NewExisted(b))
continue continue
} }
if cap(b) >= buf.Size {
mb = append(mb, buf.NewExisted(b))
} else {
nb := buf.New() nb := buf.New()
nb.Extend(int32(len(b))) nb.Extend(int32(len(b)))
copy(nb.Bytes(), b) copy(nb.Bytes(), b)
mb = append(mb, nb) mb = append(mb, nb)
} }
}
return mb, nil return mb, nil
} }
@ -99,12 +103,15 @@ func (h *MultiHunkReaderWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
return io.ErrClosedPipe return io.ErrClosedPipe
} }
hunk := &MultiHunk{Data: make([][]byte, len(mb))} hunks := make([][]byte, 0, len(mb))
for _, b := range mb { for _, b := range mb {
hunk.Data = append(hunk.Data, b.Bytes()) if b.Len() > 0 {
hunks = append(hunks, b.Bytes())
}
} }
err := h.hc.Send(hunk) err := h.hc.Send(&MultiHunk{Data: hunks})
if err != nil { if err != nil {
return err return err
} }