-
Notifications
You must be signed in to change notification settings - Fork 79
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
vm: fix packmap operation #3685
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -826,12 +826,14 @@ | |
|
||
// NewMapWithValue returns a new Map object filled with the specified value. | ||
func NewMapWithValue(value []MapElement) *Map { | ||
if value != nil { | ||
return &Map{ | ||
value: value, | ||
res := NewMap() | ||
for i := range value { | ||
if err := IsValidMapKey(value[i].Key); err != nil { | ||
panic(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's too risky to panic inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, |
||
} | ||
res.Add(value[i].Key, value[i].Value) | ||
} | ||
return NewMap() | ||
return res | ||
} | ||
|
||
// Value implements the Item interface. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,8 +385,10 @@ func TestToJSONWithTypesBadCases(t *testing.T) { | |
t.Run("overflow on map key", func(t *testing.T) { | ||
m := NewMapWithValue([]MapElement{ | ||
{NewBool(true), NewBool(true)}, | ||
{NewByteArray(bigBuf), NewBool(true)}, | ||
}) | ||
for i := 1; i < MaxSize/85; i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe MaxSerialized better |
||
m.Add(NewBigInteger(big.NewInt(int64(i))), NewBigInteger(big.NewInt(123456))) | ||
} | ||
_, err := ToJSONWithTypes(m) | ||
require.ErrorIs(t, err, errTooBigSize) | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1214,15 +1214,14 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro | |
panic("invalid length") | ||
} | ||
|
||
items := make([]stackitem.MapElement, n) | ||
for i := range n { | ||
newMap := stackitem.NewMap() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
m |
||
for range n { | ||
key := v.estack.Pop() | ||
validateMapKey(key) | ||
val := v.estack.Pop().value | ||
items[i].Key = key.value | ||
items[i].Value = val | ||
newMap.Add(key.Item(), val) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way is not optimal, verification is performed twice. |
||
} | ||
v.estack.PushItem(stackitem.NewMapWithValue(items)) | ||
v.estack.PushItem(newMap) | ||
|
||
case opcode.PACKSTRUCT, opcode.PACK: | ||
n := toInt(v.estack.Pop().BigInt()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useless change.