From 419b404de95e1e463e3f1a9793bc708d5666ef67 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 17 Sep 2021 22:00:43 +0200 Subject: [PATCH] Check for zero length requests on container --- container/container.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/container/container.go b/container/container.go index 11e732d..1753eb0 100644 --- a/container/container.go +++ b/container/container.go @@ -274,11 +274,17 @@ func (c *Container) PrependLength() { } func (c *Container) gather(n int) []byte { - // check if first slice holds enough data + // Check requested length. + if n <= 0 { + return nil + } + + // Check if the first slice holds enough data. if len(c.compartments[c.offset]) >= n { return c.compartments[c.offset][:n] } - // start gathering data + + // Start gathering data. slice := make([]byte, n) copySlice := slice n = 0 @@ -295,6 +301,13 @@ func (c *Container) gather(n int) []byte { } func (c *Container) gatherAsContainer(n int) (new *Container) { + // Check requested length. + if n < 0 { + return nil + } else if n == 0 { + return &Container{} + } + new = &Container{} for i := c.offset; i < len(c.compartments); i++ { if n >= len(c.compartments[i]) { @@ -383,7 +396,7 @@ func (c *Container) GetNextN32() (uint32, error) { // GetNextN64 parses and returns a varint of type uint64. func (c *Container) GetNextN64() (uint64, error) { - buf := c.gather(9) + buf := c.gather(10) num, n, err := varint.Unpack64(buf) if err != nil { return 0, err