From f9218bf84f4ec5d246aaa21b9c5e25d53eff1358 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 1 Sep 2021 12:55:09 +0300 Subject: [PATCH] [#787] util/attributes: Restore escape characters after caching the result As soon as resulting list and cache operate with the attribute pointer, we can add attribute structure immediately and set restored key and value of the attribute later. Signed-off-by: Alex Vanin --- pkg/util/attributes/parser.go | 8 ++++---- pkg/util/attributes/parser_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/util/attributes/parser.go b/pkg/util/attributes/parser.go index 340443bff..46562a4ca 100644 --- a/pkg/util/attributes/parser.go +++ b/pkg/util/attributes/parser.go @@ -59,16 +59,16 @@ func ParseV2Attributes(attrs []string, excl []string) ([]*netmap.NodeAttribute, } if !present { + attribute = netmap.NewNodeAttribute() + cache[key] = attribute + result = append(result, attribute) + // replace non-printable symbols with escaped symbols without escape character key = replaceEscaping(key, true) value = replaceEscaping(value, true) - attribute = netmap.NewNodeAttribute() attribute.SetKey(key) attribute.SetValue(value) - - cache[key] = attribute - result = append(result, attribute) } if parentKey != "" { diff --git a/pkg/util/attributes/parser_test.go b/pkg/util/attributes/parser_test.go index c9c0c9978..87fcef2a0 100644 --- a/pkg/util/attributes/parser_test.go +++ b/pkg/util/attributes/parser_test.go @@ -74,6 +74,13 @@ func TestParseV2Attributes(t *testing.T) { attrs, err := attributes.ParseV2Attributes(from, nil) require.NoError(t, err) require.Len(t, attrs, 1) + + t.Run("with escape characters", func(t *testing.T) { + from = []string{`/a\::b\/`, `/a\::b\/`} + attrs, err := attributes.ParseV2Attributes(from, nil) + require.NoError(t, err) + require.Len(t, attrs, 1) + }) }) t.Run("multiple parents", func(t *testing.T) {