Skip to content

Commit

Permalink
proc: fix index access to already-loaded string values (go-delve#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli authored Nov 7, 2022
1 parent b072f61 commit cba16f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion _fixtures/fncall.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,15 @@ func main() {

d := &Derived{3, Base{4}}

var ref strings.Builder
fmt.Fprintf(&ref, "blah")

runtime.Breakpoint() // breakpoint here
call1(one, two)
fn2clos(2)
strings.LastIndexByte(stringslice[1], 'w')
d.Method()
d.Base.Method()
x.CallMe()
fmt.Println(one, two, zero, call, call0, call2, callexit, callpanic, callbreak, callstacktrace, stringsJoin, intslice, stringslice, comma, a.VRcvr, a.PRcvr, pa, vable_a, vable_pa, pable_pa, fn2clos, fn2glob, fn2valmeth, fn2ptrmeth, fn2nil, ga, escapeArg, a2, square, intcallpanic, onetwothree, curriedAdd, getAStruct, getAStructPtr, getVRcvrableFromAStruct, getPRcvrableFromAStructPtr, getVRcvrableFromAStructPtr, pa2, noreturncall, str, d, x, x2.CallMe(5), longstrs, regabistacktest, regabistacktest2, issue2698.String(), regabistacktest3, rast3, floatsum)
fmt.Println(one, two, zero, call, call0, call2, callexit, callpanic, callbreak, callstacktrace, stringsJoin, intslice, stringslice, comma, a.VRcvr, a.PRcvr, pa, vable_a, vable_pa, pable_pa, fn2clos, fn2glob, fn2valmeth, fn2ptrmeth, fn2nil, ga, escapeArg, a2, square, intcallpanic, onetwothree, curriedAdd, getAStruct, getAStructPtr, getVRcvrableFromAStruct, getPRcvrableFromAStructPtr, getVRcvrableFromAStructPtr, pa2, noreturncall, str, d, x, x2.CallMe(5), longstrs, regabistacktest, regabistacktest2, issue2698.String(), regabistacktest3, rast3, floatsum, ref)
}
16 changes: 15 additions & 1 deletion pkg/proc/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2084,7 +2084,21 @@ func (v *Variable) sliceAccess(idx int) (*Variable, error) {
return nil, fmt.Errorf("index out of bounds")
}
if v.loaded {
return &v.Children[idx], nil
if v.Kind == reflect.String {
s := constant.StringVal(v.Value)
if idx >= len(s) {
return nil, fmt.Errorf("index out of bounds")
}
r := v.newVariable("", v.Base+uint64(int64(idx)*v.stride), v.fieldType, v.mem)
r.loaded = true
r.Value = constant.MakeInt64(int64(s[idx]))
return r, nil
} else {
if idx >= len(v.Children) {
return nil, fmt.Errorf("index out of bounds")
}
return &v.Children[idx], nil
}
}
mem := v.mem
if v.Kind != reflect.Array {
Expand Down
4 changes: 4 additions & 0 deletions pkg/proc/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,10 @@ func TestCallFunction(t *testing.T) {
// Issue 1577
{"1+2", []string{`::3`}, nil},
{`"de"+"mo"`, []string{`::"demo"`}, nil},

// Issue 3176
{`ref.String()[0]`, []string{`:byte:98`}, nil},
{`ref.String()[20]`, nil, errors.New("index out of bounds")},
}

var testcases112 = []testCaseCallFunction{
Expand Down

0 comments on commit cba16f9

Please sign in to comment.