Skip to content

ComputeMapKey can yield the same result for different values #4567

@thehowl

Description

@thehowl

Something that has been bothering me for a while, but I just needed to come up with a MVP.

package main

func main() {
	x := map[any]string{}
	k1 := [2]string{"hi,wor", "ld"}
	k2 := [2]string{"hi", "wor,ld"}
	x[k1] = "123"
	x[k2] = "456"
	println(x)
}

// Output:
// map{(array[("hi,wor" string),("ld" string)] [2]string):("456" string)}

Note the single value printed, in spite of the two keys.

ComputeMapKey yields the same value ([2]string:[hi,wor,ld]), so it's easy to trick.

More examples:

package main

func main() {
	x := map[any]string{}
	k1 := [2]any{"hi,string:wor", "ld"}
	k2 := [2]any{"hi", "wor,string:ld"}
	x[k1] = "123"
	x[k2] = "456"
	println(x)
}

The easiest way is probably to make strings have a length prefix, which I think should make it impossible to make strings "escape" properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    📦 🤖 gnovmIssues or PRs gnovm related🐞 bugSomething isn't working

    Type

    No type

    Projects

    Status

    Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions