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: account for recursion when stringing to avoid overflow #1315

Merged
merged 21 commits into from
Dec 21, 2023
Merged
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
172 changes: 120 additions & 52 deletions gnovm/pkg/gnolang/values_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,115 @@
"strings"
)

const recursed string = "<recursed>"

func (v StringValue) String() string {
return strconv.Quote(string(v))
}

func (bv BigintValue) String() string {
return bv.V.String()
func (v BigintValue) String() string {
return v.V.String()
}

func (v BigdecValue) String() string {
return v.V.String()
}

func (bv BigdecValue) String() string {
return bv.V.String()
func (v DataByteValue) String() string {
return fmt.Sprintf("(%0X)", (v.GetByte()))
}

func (dbv DataByteValue) String() string {
return fmt.Sprintf("(%0X)", (dbv.GetByte()))
func (v *ArrayValue) String() string {
return v.ProtectedString(map[Value]struct{}{})
}

func (av *ArrayValue) String() string {
ss := make([]string, len(av.List))
if av.Data == nil {
for i, e := range av.List {
ss[i] = e.String()
func (v *ArrayValue) ProtectedString(seen map[Value]struct{}) string {
if _, ok := seen[v]; ok {
return recursed
}

seen[v] = struct{}{}
ss := make([]string, len(v.List))
if v.Data == nil {
for i, e := range v.List {
ss[i] = e.ProtectedString(seen)
}
// NOTE: we may want to unify the representation,
// but for now tests expect this to be different.
// This may be helpful for testing implementation behavior.
return "array[" + strings.Join(ss, ",") + "]"
}
if len(av.Data) > 256 {
return fmt.Sprintf("array[0x%X...]", av.Data[:256])
if len(v.Data) > 256 {
return fmt.Sprintf("array[0x%X...]", v.Data[:256])
}
return fmt.Sprintf("array[0x%X]", av.Data)
return fmt.Sprintf("array[0x%X]", v.Data)
}

func (sv *SliceValue) String() string {
if sv.Base == nil {
func (v *SliceValue) String() string {
return v.ProtectedString(map[Value]struct{}{})
}

func (v *SliceValue) ProtectedString(seen map[Value]struct{}) string {
if v.Base == nil {
return "nil-slice"
}
if ref, ok := sv.Base.(RefValue); ok {

if _, ok := seen[v]; ok {
return recursed
}

if ref, ok := v.Base.(RefValue); ok {
return fmt.Sprintf("slice[%v]", ref)
}
vbase := sv.Base.(*ArrayValue)

seen[v] = struct{}{}
vbase := v.Base.(*ArrayValue)
if vbase.Data == nil {
ss := make([]string, sv.Length)
for i, e := range vbase.List[sv.Offset : sv.Offset+sv.Length] {
ss[i] = e.String()
ss := make([]string, v.Length)
for i, e := range vbase.List[v.Offset : v.Offset+v.Length] {
ss[i] = e.ProtectedString(seen)
}
return "slice[" + strings.Join(ss, ",") + "]"
}
if sv.Length > 256 {
return fmt.Sprintf("slice[0x%X...(%d)]", vbase.Data[sv.Offset:sv.Offset+256], sv.Length)
if v.Length > 256 {
return fmt.Sprintf("slice[0x%X...(%d)]", vbase.Data[v.Offset:v.Offset+256], v.Length)
}
return fmt.Sprintf("slice[0x%X]", vbase.Data[sv.Offset:sv.Offset+sv.Length])
return fmt.Sprintf("slice[0x%X]", vbase.Data[v.Offset:v.Offset+v.Length])
}

func (pv PointerValue) String() string {
// NOTE: cannot do below, due to recursion problems.
// TODO: create a different String2(...) function.
// return fmt.Sprintf("&%s", v.TypedValue.String())
return fmt.Sprintf("&%p.(*%s)", pv.TV, pv.TV.T.String())
func (v PointerValue) String() string {
return v.ProtectedString(map[Value]struct{}{})
}

func (sv *StructValue) String() string {
ss := make([]string, len(sv.Fields))
for i, f := range sv.Fields {
ss[i] = f.String()
func (v PointerValue) ProtectedString(seen map[Value]struct{}) string {
if _, ok := seen[v]; ok {
return recursed
}

Check warning on line 93 in gnovm/pkg/gnolang/values_string.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values_string.go#L93

Added line #L93 was not covered by tests

seen[v] = struct{}{}
return fmt.Sprintf("&%s", v.TV.ProtectedString(seen))
}

func (v *StructValue) String() string {
return v.ProtectedString(map[Value]struct{}{})
}

func (v *StructValue) ProtectedString(seen map[Value]struct{}) string {
if _, ok := seen[v]; ok {
return recursed
}

seen[v] = struct{}{}
ss := make([]string, len(v.Fields))
for i, f := range v.Fields {
ss[i] = f.ProtectedString(seen)
}
return "struct{" + strings.Join(ss, ",") + "}"
}

func (fv *FuncValue) String() string {
name := string(fv.Name)
if fv.Type == nil {
func (v *FuncValue) String() string {
name := string(v.Name)
if v.Type == nil {
return fmt.Sprintf("incomplete-func ?%s(?)?", name)
}
return name
Expand All @@ -103,12 +140,22 @@
recvT, name, params, results)
}

func (mv *MapValue) String() string {
if mv.List == nil {
func (v *MapValue) String() string {
return v.ProtectedString(map[Value]struct{}{})
}

func (v *MapValue) ProtectedString(seen map[Value]struct{}) string {
if v.List == nil {
return "zero-map"
}
ss := make([]string, 0, mv.GetLength())
next := mv.List.Head

if _, ok := seen[v]; ok {
return recursed
}

seen[v] = struct{}{}
ss := make([]string, 0, v.GetLength())
next := v.List.Head
for next != nil {
ss = append(ss,
next.Key.String()+":"+
Expand All @@ -133,13 +180,13 @@
v.Type.String(), ptr)
}

func (pv *PackageValue) String() string {
return fmt.Sprintf("package(%s %s)", pv.PkgName, pv.PkgPath)
func (v *PackageValue) String() string {
return fmt.Sprintf("package(%s %s)", v.PkgName, v.PkgPath)
}

func (nv *NativeValue) String() string {
func (v *NativeValue) String() string {
return fmt.Sprintf("gonative{%v}",
nv.Value.Interface())
v.Value.Interface())
/*
return fmt.Sprintf("gonative{%v (%s)}",
v.Value.Interface(),
Expand Down Expand Up @@ -176,10 +223,21 @@
res := m.Eval(Call(Sel(&ConstExpr{TypedValue: *tv}, "Error")))
return res[0].GetString()
}

return tv.ProtectedSprint(map[Value]struct{}{}, true)
}

func (tv *TypedValue) ProtectedSprint(seen map[Value]struct{}, considerDeclaredType bool) string {

if _, ok := seen[tv.V]; ok {
return recursed
}

// print declared type
if _, ok := tv.T.(*DeclaredType); ok {
return tv.String()
if _, ok := tv.T.(*DeclaredType); ok && considerDeclaredType {
return tv.ProtectedString(seen)
}

// otherwise, default behavior.
switch bt := baseOf(tv.T).(type) {
case PrimitiveType:
Expand Down Expand Up @@ -223,15 +281,15 @@
if tv.V == nil {
return "invalid-pointer"
}
return tv.V.(PointerValue).String()
return tv.V.(PointerValue).ProtectedString(seen)
case *ArrayType:
return tv.V.(*ArrayValue).String()
return tv.V.(*ArrayValue).ProtectedString(seen)
case *SliceType:
return tv.V.(*SliceValue).String()
return tv.V.(*SliceValue).ProtectedString(seen)
case *StructType:
return tv.V.(*StructValue).String()
return tv.V.(*StructValue).ProtectedString(seen)
case *MapType:
return tv.V.(*MapValue).String()
return tv.V.(*MapValue).ProtectedString(seen)
case *FuncType:
switch fv := tv.V.(type) {
case nil:
Expand Down Expand Up @@ -281,6 +339,10 @@

// For gno debugging/testing.
func (tv TypedValue) String() string {
return tv.ProtectedString(map[Value]struct{}{})
}

func (tv TypedValue) ProtectedString(seen map[Value]struct{}) string {
if tv.IsUndefined() {
return "(undefined)"
}
Expand Down Expand Up @@ -317,12 +379,18 @@
vs = fmt.Sprintf("%v", tv.GetFloat32())
case Float64Type:
vs = fmt.Sprintf("%v", tv.GetFloat64())
// Complex types that require recusion protection.
default:
vs = nilStr
}
} else {
vs = fmt.Sprintf("%v", tv.V)
// vs = fmt.Sprintf("%v", tv.V)
deelawn marked this conversation as resolved.
Show resolved Hide resolved
vs = tv.ProtectedSprint(seen, false)
if base := baseOf(tv.T); base == StringType || base == UntypedStringType {
vs = strconv.Quote(vs)
}
}

ts := tv.T.String()
return fmt.Sprintf("(%s %s)", vs, ts) // TODO improve
}
Loading