Item cast to struct does not ensure/convert field type #1383

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

Originally created by @carpawell on GitHub (Oct 11, 2024).

Tried to cast item from contract's storage and use it.

Current Behavior

	newNodes := storage.Find(ctx, newNodesPrefix, storage.None)
	for iterator.Next(newNodes) {
		newNode := iterator.Value(newNodes).(struct {
			key []byte
			val []byte
		})

		newNode.key[0] = 1
	}

leads to SETITEM: invalid item type ByteString

newNode.key[:][0] = 1 solves the issue... why?

Expected Behavior

I said (asserted/converted) that it is a []byte, i want to use it as []byte.

Possible Solution

Ensure, cast or throw an exception at .() operator, not when it is tried to be used later.

Steps to Reproduce

Code from Current Behavior.

Context

https://github.com/nspcc-dev/neofs-contract/pull/438#discussion_r1786975526

Regression

Not sure.

Your Environment

github.com/nspcc-dev/neo-go v0.106.3

Originally created by @carpawell on GitHub (Oct 11, 2024). Tried to cast item from contract's storage and use it. ## Current Behavior ```go newNodes := storage.Find(ctx, newNodesPrefix, storage.None) for iterator.Next(newNodes) { newNode := iterator.Value(newNodes).(struct { key []byte val []byte }) newNode.key[0] = 1 } ``` leads to ` SETITEM: invalid item type ByteString` `newNode.key[:][0] = 1` solves the issue... why? ## Expected Behavior I said (asserted/converted) that it is a `[]byte`, i want to use it as `[]byte`. ## Possible Solution Ensure, cast or throw an exception at `.()` operator, not when it is tried to be used later. ## Steps to Reproduce Code from `Current Behavior`. ## Context https://github.com/nspcc-dev/neofs-contract/pull/438#discussion_r1786975526 ## Regression Not sure. ## Your Environment github.com/nspcc-dev/neo-go v0.106.3
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#1383
No description provided.