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

fix(gnovm): fix max names in block check #2645

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/parser"
"go/token"
"math"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -1802,7 +1803,7 @@ func (sb *StaticBlock) Define2(isConst bool, n Name, st Type, tv TypedValue) {
if int(sb.NumNames) != len(sb.Names) {
panic("StaticBlock.NumNames and len(.Names) mismatch")
}
if (1<<16 - 1) < sb.NumNames {
if sb.NumNames == math.MaxUint16 {
panic("too many variables in block")
}
if tv.T == nil && tv.V != nil {
Expand Down
44 changes: 44 additions & 0 deletions gnovm/pkg/gnolang/nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package gnolang_test

import (
"math"
"testing"

"github.com/gnolang/gno/gnovm/pkg/gnolang"
)

func TestStaticBlock_Define2_MaxNames(t *testing.T) {
defer func() {
if r := recover(); r != nil {
panicString, ok := r.(string)
if !ok {
t.Errorf("expected panic string, got %v", r)
}

if panicString != "too many variables in block" {
t.Errorf("expected panic string to be 'too many variables in block', got '%s'", panicString)
}

return
}

// If it didn't panic, fail.
t.Errorf("expected panic when exceeding maximum number of names")
}()

staticBlock := new(gnolang.StaticBlock)
staticBlock.NumNames = math.MaxUint16 - 1
staticBlock.Names = make([]gnolang.Name, staticBlock.NumNames)

// Adding one more is okay.
staticBlock.Define2(false, gnolang.Name("a"), gnolang.BoolType, gnolang.TypedValue{T: gnolang.BoolType})
if staticBlock.NumNames != math.MaxUint16 {
t.Errorf("expected NumNames to be %d, got %d", math.MaxUint16, staticBlock.NumNames)
}
if len(staticBlock.Names) != math.MaxUint16 {
t.Errorf("expected len(Names) to be %d, got %d", math.MaxUint16, len(staticBlock.Names))
}

// This one should panic because the maximum number of names has been reached.
staticBlock.Define2(false, gnolang.Name("a"), gnolang.BoolType, gnolang.TypedValue{T: gnolang.BoolType})
}
Loading