go-capnp icon indicating copy to clipboard operation
go-capnp copied to clipboard

Panic on all zero stream

Open bytescreator opened this issue 5 months ago • 0 comments

When a stream containing all zero bytes is decoded by capnp.Decoder, a message which contains a zero length segment causes message.Root to panic whilst reading the message. I don't know that this is intended behaviour so i reported just in case.

Possible cause is when Segment.root is called by Message.Root; Segment.root returns a zero value PointerList (due to out of bounds) which causes a panic in List.primitiveElem

Reproducing code

package main

import (
	"capnproto.org/go/capnp/v3"
)

type zeros struct{}

func (zeros) Read(b []byte) (int, error) {
	for i := 0; i < len(b); i++ {
		b[i] = 0
	}
	return len(b), nil
}

func main() {
	m, err := capnp.NewDecoder(zeros{}).Decode()
	if err != nil {
		panic(err)
	}

	m.Root()
}

panic

panic: list element out of bounds

goroutine 1 [running]:
capnproto.org/go/capnp/v3.List.primitiveElem({0x0, 0x0, 0x0, {0x0, 0x0}, 0x0, 0x0}, 0xc00011de20?, {0x0, 0x1})
        $GOPATH/pkg/mod/capnproto.org/go/capnp/[email protected]/list.go:190 +0x1a8
capnproto.org/go/capnp/v3.PointerList.primitiveElem(...)
        $GOPATH/pkg/mod/capnproto.org/go/capnp/[email protected]/list-gen.go:226
capnproto.org/go/capnp/v3.PointerList.At({0x0, 0x0, 0x0, {0x0, 0x0}, 0x0, 0x0}, 0xbfd445?)
        $GOPATH/pkg/mod/capnproto.org/go/capnp/[email protected]/list.go:356 +0xbf
capnproto.org/go/capnp/v3.(*Message).Root(0xc00007c080?)
        $GOPATH/pkg/mod/capnproto.org/go/capnp/[email protected]/message.go:202 +0xe8
main.main()
        $GOPATH/src/<REDACTED>/test.go:22 +0x3e
exit status 2

go.mod

module capnptest

go 1.22.2

require capnproto.org/go/capnp/v3 v3.0.1-alpha.2

require (
	github.com/colega/zeropool v0.0.0-20230505084239-6fb4a4f75381 // indirect
	golang.org/x/sync v0.7.0 // indirect
)

bytescreator avatar Sep 05 '24 16:09 bytescreator