From 212c2002bb28e2b6f77cff645ae81b29dfaced41 Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Wed, 1 Mar 2023 20:27:06 +0100 Subject: [PATCH] proc: do not try to load a non-empty slice if the base address is 0 (#3295) --- _fixtures/testvariables2.go | 7 ++++++- pkg/proc/variables.go | 4 ++++ pkg/proc/variables_test.go | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/_fixtures/testvariables2.go b/_fixtures/testvariables2.go index 5ee588e2..26439c9d 100644 --- a/_fixtures/testvariables2.go +++ b/_fixtures/testvariables2.go @@ -4,6 +4,7 @@ import ( "fmt" "go/constant" "math" + "reflect" "runtime" "time" "unsafe" @@ -371,6 +372,10 @@ func main() { namedA1 := astructName1{12, 45} namedA2 := astructName2{13, 46} + badslice := []int{1, 2, 3} + h := (*reflect.SliceHeader)(unsafe.Pointer(&badslice)) + h.Data = 0 + var amb1 = 1 runtime.Breakpoint() for amb1 := 0; amb1 < 10; amb1++ { @@ -381,5 +386,5 @@ func main() { longslice := make([]int, 100, 100) runtime.Breakpoint() - fmt.Println(i1, i2, i3, p1, pp1, amb1, s1, s3, a0, a1, p2, p3, s2, as1, str1, f1, fn1, fn2, nilslice, nilptr, ch1, chnil, m1, mnil, m2, m3, m4, m5, upnil, up1, i4, i5, i6, err1, err2, errnil, iface1, iface2, ifacenil, arr1, parr, cpx1, const1, iface3, iface4, recursive1, recursive1.x, iface5, iface2fn1, iface2fn2, bencharr, benchparr, mapinf, mainMenu, b, b2, sd, anonstruct1, anonstruct2, anoniface1, anonfunc, mapanonstruct1, ifacearr, efacearr, ni8, ni16, ni32, ni64, pinf, ninf, nan, zsvmap, zsslice, zsvar, tm, rettm, errtypednil, emptyslice, emptymap, byteslice, bytestypeslice, runeslice, bytearray, bytetypearray, runearray, longstr, nilstruct, as2, as2.NonPointerRecieverMethod, s4, iface2map, issue1578, ll, unread, w2, w3, w4, w5, longarr, longslice, val, m6, m7, cl, tim1, tim2, typedstringvar, namedA1, namedA2, astructName1(namedA2)) + fmt.Println(i1, i2, i3, p1, pp1, amb1, s1, s3, a0, a1, p2, p3, s2, as1, str1, f1, fn1, fn2, nilslice, nilptr, ch1, chnil, m1, mnil, m2, m3, m4, m5, upnil, up1, i4, i5, i6, err1, err2, errnil, iface1, iface2, ifacenil, arr1, parr, cpx1, const1, iface3, iface4, recursive1, recursive1.x, iface5, iface2fn1, iface2fn2, bencharr, benchparr, mapinf, mainMenu, b, b2, sd, anonstruct1, anonstruct2, anoniface1, anonfunc, mapanonstruct1, ifacearr, efacearr, ni8, ni16, ni32, ni64, pinf, ninf, nan, zsvmap, zsslice, zsvar, tm, rettm, errtypednil, emptyslice, emptymap, byteslice, bytestypeslice, runeslice, bytearray, bytetypearray, runearray, longstr, nilstruct, as2, as2.NonPointerRecieverMethod, s4, iface2map, issue1578, ll, unread, w2, w3, w4, w5, longarr, longslice, val, m6, m7, cl, tim1, tim2, typedstringvar, namedA1, namedA2, astructName1(namedA2), badslice) } diff --git a/pkg/proc/variables.go b/pkg/proc/variables.go index e24c8240..b1dc27e5 100644 --- a/pkg/proc/variables.go +++ b/pkg/proc/variables.go @@ -1656,6 +1656,10 @@ func (v *Variable) loadArrayValues(recurseLevel int, cfg LoadConfig) { v.Unreadable = errors.New("Negative array length") return } + if v.Base == 0 && v.Len > 0 { + v.Unreadable = errors.New("non-zero length array with nil base") + return + } count := v.Len // Cap number of elements diff --git a/pkg/proc/variables_test.go b/pkg/proc/variables_test.go index 9cd993a1..dab345b4 100644 --- a/pkg/proc/variables_test.go +++ b/pkg/proc/variables_test.go @@ -829,6 +829,9 @@ func TestEvalExpression(t *testing.T) { // Conversions to ptr-to-ptr types {`**(**runtime.hmap)(uintptr(&m1))`, false, `…`, `…`, "runtime.hmap", nil}, + + // Malformed values + {`badslice`, false, `(unreadable non-zero length array with nil base)`, `(unreadable non-zero length array with nil base)`, "[]int", nil}, } ver, _ := goversion.Parse(runtime.Version())