From db3e5ef2cdcf214eabbccda44812eca701ebea60 Mon Sep 17 00:00:00 2001 From: Alessandro Arzilli Date: Fri, 24 Jun 2022 15:48:16 +0200 Subject: [PATCH] proc: map access with string literal key should always succeed (#3036) When doing a map lookup with a string literal we should load as much of the keys to at least match the length of the string literal, so that the lookup doesn't fail with the "string too long" error. Fixes #3034 --- pkg/proc/eval.go | 10 +++++++++- pkg/proc/variables_test.go | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/proc/eval.go b/pkg/proc/eval.go index 24c5a6bd..61109352 100644 --- a/pkg/proc/eval.go +++ b/pkg/proc/eval.go @@ -2038,10 +2038,18 @@ func (v *Variable) mapAccess(idx *Variable) (*Variable, error) { return nil, fmt.Errorf("can not access unreadable map: %v", v.Unreadable) } + lcfg := loadFullValue + if idx.Kind == reflect.String && int64(len(constant.StringVal(idx.Value))) == idx.Len && idx.Len > int64(lcfg.MaxStringLen) { + // If the index is a string load as much of the keys to at least match the length of the index. + //TODO(aarzilli): when struct literals are implemented this needs to be + //done recursively for literal struct fields. + lcfg.MaxStringLen = int(idx.Len) + } + first := true for it.next() { key := it.key() - key.loadValue(loadFullValue) + key.loadValue(lcfg) if key.Unreadable != nil { return nil, fmt.Errorf("can not access unreadable map: %v", key.Unreadable) } diff --git a/pkg/proc/variables_test.go b/pkg/proc/variables_test.go index 13c4c445..145364e8 100644 --- a/pkg/proc/variables_test.go +++ b/pkg/proc/variables_test.go @@ -794,6 +794,9 @@ func TestEvalExpression(t *testing.T) { // pretty printing special types {"tim1", false, `time.Time(1977-05-25T18:00:00Z)…`, `time.Time(1977-05-25T18:00:00Z)…`, "time.Time", nil}, {"tim2", false, `time.Time(2022-06-07T02:03:04-06:00)…`, `time.Time(2022-06-07T02:03:04-06:00)…`, "time.Time", nil}, + + // issue #3034 - map access with long string key + {`m6["very long string 0123456789a0123456789b0123456789c0123456789d0123456789e0123456789f0123456789g012345678h90123456789i0123456789j0123456789"]`, false, `123`, `123`, "int", nil}, } ver, _ := goversion.Parse(runtime.Version())