From cc0becf58cd0d5916b87770370fee7bef1e927d1 Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 9 Feb 2019 22:25:36 +1030 Subject: [PATCH] stream/mts/meta: simplified meta checking for Get and GetAll --- stream/mts/meta/meta.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/stream/mts/meta/meta.go b/stream/mts/meta/meta.go index d69a2b60..4b5cf8e9 100644 --- a/stream/mts/meta/meta.go +++ b/stream/mts/meta/meta.go @@ -50,8 +50,7 @@ const ( var ( errKeyAbsent = errors.New("Key does not exist in map") - errNoHeader = errors.New("Metadata string does not contain header") - errInvalidHeader = errors.New("Metadata string does not contain valid header") + errInvalidMeta = errors.New("Invalid metadata given") errUnexpectedMetaFormat = errors.New("Unexpected meta format") ) @@ -167,7 +166,7 @@ func (m *Data) Encode() []byte { // Get returns the value for the given key in d. func Get(key string, d []byte) (string, error) { - err := checkHeader(d) + err := checkMeta(d) if err != nil { return "", err } @@ -184,13 +183,7 @@ func Get(key string, d []byte) (string, error) { // GetAll returns metadata keys and values from d. func GetAll(d []byte) ([][2]string, error) { - if d == nil { - return nil, errors.New("nil slice given") - } - if len(d) == 0 { - return nil, errors.New("empty slice given") - } - err := checkHeader(d) + err := checkMeta(d) if err != nil { return nil, err } @@ -208,11 +201,9 @@ func GetAll(d []byte) ([][2]string, error) { } // checkHeader checks that a valid metadata header exists in the given data. -func checkHeader(d []byte) error { - if d[0] != 0 { - return errNoHeader - } else if d[0] == 0 && binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) { - return errInvalidHeader +func checkMeta(d []byte) error { + if len(d) == 0 || d[0] != 0 || binary.BigEndian.Uint16(d[2:headSize]) != uint16(len(d[headSize:])) { + return errInvalidMeta } return nil }