From 8ecea8221fe5158b769d03238b32bc4a2772ee81 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 12 Apr 2021 19:17:01 +0300 Subject: [PATCH] fix nil buf check Signed-off-by: Vasiliy Tolstov --- json/json.go | 4 +++- proto/proto.go | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/json/json.go b/json/json.go index 2572491..e17938e 100644 --- a/json/json.go +++ b/json/json.go @@ -53,7 +53,7 @@ func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) { } func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error { - if b == nil { + if len(b) == 0 { return nil } switch m := v.(type) { @@ -80,6 +80,8 @@ func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error { buf, err := ioutil.ReadAll(conn) if err != nil { return err + } else if len(buf) == 0 { + return nil } m.Data = buf return nil diff --git a/proto/proto.go b/proto/proto.go index bce50d7..35bcf86 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -54,6 +54,8 @@ func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error { buf, err := ioutil.ReadAll(conn) if err != nil { return err + } else if len(buf) == 0 { + return nil } m.Data = buf return nil @@ -61,7 +63,7 @@ func (c *protoCodec) ReadBody(conn io.Reader, b interface{}) error { buf, err := ioutil.ReadAll(conn) if err != nil { return err - } else if buf == nil { + } else if len(buf) == 0 { return nil } return proto.Unmarshal(buf, m)