Skip to content

Commit

Permalink
fix: PUSH([]T{}) is noop for empty slice (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N authored Aug 3, 2024
1 parent 0029c19 commit 697c617
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pushlabels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ func TestPUSHLabels(t *testing.T) {
// t.Logf("want: %d %#x", len(want), want)
}

func TestPUSHNoLabels(t *testing.T) {
// Arbitrary boundaries to surround the empty push; MUST have no effect on
// the stack otherwise compilation will fail.
const (
before = MSIZE
after = GAS
)

code := Code{
before,
PUSH([]JUMPDEST{}),
PUSH([]Label{}),
PUSH([]string{}),
after,
}
want := asBytes(before, after)

t.Logf("%T = {%s, PUSH([]JUMPDEST{}, []Label{}, []string), %s}", code, before, after)

got, err := code.Compile()
if err != nil {
t.Fatalf("%T.Compile() error %v", code, err)
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("%T.Compile() diff (-want +got):\n%s", code, diff)
}
}

type opCode interface {
vm.OpCode | types.OpCode
}
Expand Down
5 changes: 5 additions & 0 deletions specops.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/binary"
"fmt"
"math/bits"
"reflect"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -87,6 +88,10 @@ func PUSH[P interface {
) types.Bytecoder {
pToB := types.BytecoderFromStackPusher

if val := reflect.ValueOf(v); val.Kind() == reflect.Slice && val.Len() == 0 {
return Raw{}
}

switch v := any(v).(type) {
case int:
if v < 0 {
Expand Down

0 comments on commit 697c617

Please sign in to comment.