From 4ce426c81a90980e2d6863d1eb814016c1ab6f89 Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Sat, 1 Oct 2022 16:26:26 +0800 Subject: [PATCH] make a copy when retrieving json path expression in cache Signed-off-by: xiongjiwei --- types/json_path_expr.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/types/json_path_expr.go b/types/json_path_expr.go index bed9cba95f831..9c66b3254114c 100644 --- a/types/json_path_expr.go +++ b/types/json_path_expr.go @@ -92,6 +92,12 @@ type JSONPathExpression struct { flags jsonPathExpressionFlag } +func (pe JSONPathExpression) clone() JSONPathExpression { + legs := make([]jsonPathLeg, len(pe.legs)) + copy(legs, pe.legs) + return JSONPathExpression{legs: legs, flags: pe.flags} +} + var peCache JSONPathExpressionCache type jsonPathExpressionKey string @@ -362,7 +368,7 @@ func ParseJSONPathExpr(pathExpr string) (JSONPathExpression, error) { val, ok := peCache.cache.Get(jsonPathExpressionKey(pathExpr)) if ok { peCache.mu.Unlock() - return val.(JSONPathExpression), nil + return val.(JSONPathExpression).clone(), nil } peCache.mu.Unlock()