Panic inside io.PutVarUint #1457

Open
opened 2025-12-28 17:16:31 +00:00 by sami · 3 comments
Owner

Originally created by @cthulhu-rider on GitHub (Dec 18, 2024).

Current Behavior

panic: runtime error: index out of range [8] with length 5 [recovered]
	panic: runtime error: index out of range [8] with length 5

goroutine 19 [running]:
testing.tRunner.func1.2({0x5dd0c0, 0xc0000f6180})
	/usr/local/go/src/testing/testing.go:1631 +0x24a
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1634 +0x377
panic({0x5dd0c0?, 0xc0000f6180?})
	/usr/local/go/src/runtime/panic.go:770 +0x132
github.com/nspcc-dev/neo-go/pkg/io.PutVarUint(...)
	/home/ll/projects/neo/go/pkg/io/binaryWriter.go:109
github.com/nspcc-dev/neo-go/pkg/io_test.TestPutVarUint(0xc0000d8b60?)

Expected Behavior

no panic when buffer has enough len, only otherwise (similar to https://pkg.go.dev/encoding/binary#PutUvarint)

Possible Solution

do not do nspcc-dev/neo-go@9189b3eb7d/pkg/io/binaryWriter.go (L109)

Steps to Reproduce

func TestPutVarUint(t *testing.T) {
	const n = uint64(123456787890)
	io.PutVarUint(make([]byte, io.GetVarSize(n)), n)
}

Regression

no

Your Environment

  • Version of the product used: v0.107.2
Originally created by @cthulhu-rider on GitHub (Dec 18, 2024). ## Current Behavior ``` panic: runtime error: index out of range [8] with length 5 [recovered] panic: runtime error: index out of range [8] with length 5 goroutine 19 [running]: testing.tRunner.func1.2({0x5dd0c0, 0xc0000f6180}) /usr/local/go/src/testing/testing.go:1631 +0x24a testing.tRunner.func1() /usr/local/go/src/testing/testing.go:1634 +0x377 panic({0x5dd0c0?, 0xc0000f6180?}) /usr/local/go/src/runtime/panic.go:770 +0x132 github.com/nspcc-dev/neo-go/pkg/io.PutVarUint(...) /home/ll/projects/neo/go/pkg/io/binaryWriter.go:109 github.com/nspcc-dev/neo-go/pkg/io_test.TestPutVarUint(0xc0000d8b60?) ``` ## Expected Behavior no panic when buffer has enough len, only otherwise (similar to https://pkg.go.dev/encoding/binary#PutUvarint) ## Possible Solution do not do https://github.com/nspcc-dev/neo-go/blob/9189b3eb7dcab1a86d8acc66269e3826c4c75515/pkg/io/binaryWriter.go#L109 ## Steps to Reproduce ```go func TestPutVarUint(t *testing.T) { const n = uint64(123456787890) io.PutVarUint(make([]byte, io.GetVarSize(n)), n) } ``` ## Regression no ## Your Environment * Version of the product used: v0.107.2
Author
Owner

@roman-khimov commented on GitHub (Dec 18, 2024):

This one was added specifically as canary (17a3f17c74). At the same time users of this API can expect some symmetry between GetVarSize() and PutVarUint(). And it'll fail later if buffer is too short.

@roman-khimov commented on GitHub (Dec 18, 2024): This one was added specifically as canary (17a3f17c74). At the same time users of this API can expect some symmetry between `GetVarSize()` and `PutVarUint()`. And it'll fail later if buffer is too short.
Author
Owner

@cthulhu-rider commented on GitHub (Dec 18, 2024):

io.GetVarSize(uint64(math.MaxInt + 1)) returns 1 which is also unexpected to me. Caused by cast nspcc-dev/neo-go@9189b3eb7d/pkg/io/size.go (L58)

@cthulhu-rider commented on GitHub (Dec 18, 2024): `io.GetVarSize(uint64(math.MaxInt + 1))` returns `1` which is also unexpected to me. Caused by cast https://github.com/nspcc-dev/neo-go/blob/9189b3eb7dcab1a86d8acc66269e3826c4c75515/pkg/io/size.go#L58
Author
Owner

@cthulhu-rider commented on GitHub (Dec 18, 2024):

At the same time users of this API can expect some symmetry between GetVarSize() and PutVarUint().

exactly. But this is unexpected even within oneself:

func TestPutVarUint(t *testing.T) {
	const n = uint64(123)
	b := make([]byte, 32)
	ln := io.PutVarUint(b, n)
	io.PutVarUint(b[:ln], n)
}
panic: runtime error: index out of range [8] with length 1 [recovered]
	panic: runtime error: index out of range [8] with length 1
@cthulhu-rider commented on GitHub (Dec 18, 2024): > At the same time users of this API can expect some symmetry between GetVarSize() and PutVarUint(). exactly. But this is unexpected even within oneself: ```go func TestPutVarUint(t *testing.T) { const n = uint64(123) b := make([]byte, 32) ln := io.PutVarUint(b, n) io.PutVarUint(b[:ln], n) } ``` ``` panic: runtime error: index out of range [8] with length 1 [recovered] panic: runtime error: index out of range [8] with length 1 ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
nspcc-dev/neo-go#1457
No description provided.