Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching map values with ok flag fails with panic #3716

Open
carpawell opened this issue Dec 2, 2024 · 3 comments
Open

Fetching map values with ok flag fails with panic #3716

carpawell opened this issue Dec 2, 2024 · 3 comments
Labels
compiler Go smart contract compiler enhancement Improving existing functionality I3 Minimal impact S4 Routine U3 Regular

Comments

@carpawell
Copy link
Member

func getFromMap(m map[string]any, key string) any {
	v, ok := m[key]
	if !ok {
		panic("'" + key + "'" + " not found")
	}

	return v
}

Current Behavior

Want safely check if map contains some value.

Expected Behavior

First, it panics: at instruction 2647 (STLOC1): runtime error: index out of range [-1]. Second, it does not work overall, why?

Possible Solution

Fix panic, support safe value fetching.

Steps to Reproduce

Try to compile snippet above.

Context

nspcc-dev/neofs-contract#448

Regression

Not sure.

Your Environment

v0.106.3

@carpawell carpawell added bug Something isn't working compiler Go smart contract compiler labels Dec 2, 2024
@AnnaShaleva
Copy link
Member

AnnaShaleva commented Dec 2, 2024

We may use HASKEY for that in compiler. As a temporary solution, you may try to use neogointernal.Ocode2("HASKEY", m, key).(bool).

@AnnaShaleva AnnaShaleva added I3 Minimal impact U3 Regular enhancement Improving existing functionality S4 Routine and removed bug Something isn't working labels Dec 2, 2024
@carpawell
Copy link
Member Author

Moreover, it is not possible to fetch missing value without exception: at instruction 2645 (PICKITEM): unhandled exception: "Key not found in Map". IMO, Map type is broken.

@roman-khimov
Copy link
Member

Looks like we've never used maps in contracts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Go smart contract compiler enhancement Improving existing functionality I3 Minimal impact S4 Routine U3 Regular
Projects
None yet
Development

No branches or pull requests

3 participants