From 6b50e04bd5307f6e9402a69bab55d358afcd72e3 Mon Sep 17 00:00:00 2001 From: Ciara Stacke Date: Mon, 21 Aug 2023 09:18:21 +0100 Subject: [PATCH 1/2] Fix go lint --- internal/mode/static/nginx/config/servers.go | 4 ++-- internal/mode/static/nginx/config/servers_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/mode/static/nginx/config/servers.go b/internal/mode/static/nginx/config/servers.go index ecd719f1ff..cd62e68871 100644 --- a/internal/mode/static/nginx/config/servers.go +++ b/internal/mode/static/nginx/config/servers.go @@ -338,7 +338,7 @@ func createHTTPMatch(match dataplane.Match, redirectPath string) httpMatch { // The name and values are delimited by "=". A name and value can always be recovered using strings.SplitN(arg,"=", 2). // Query Parameters are case-sensitive so case is preserved. func createQueryParamKeyValString(p dataplane.HTTPQueryParamMatch) string { - return string(p.Name) + "=" + p.Value + return p.Name + "=" + p.Value } // The name and values are delimited by ":". A name and value can always be recovered using strings.Split(arg, ":"). @@ -347,7 +347,7 @@ func createQueryParamKeyValString(p dataplane.HTTPQueryParamMatch) string { // We preserve the case of the name here because NGINX allows us to look up the header names in a case-insensitive // manner. func createHeaderKeyValString(h dataplane.HTTPHeaderMatch) string { - return string(h.Name) + HeaderMatchSeparator + h.Value + return h.Name + HeaderMatchSeparator + h.Value } func isPathOnlyMatch(match dataplane.Match) bool { diff --git a/internal/mode/static/nginx/config/servers_test.go b/internal/mode/static/nginx/config/servers_test.go index 7c77baea5e..bf0be01065 100644 --- a/internal/mode/static/nginx/config/servers_test.go +++ b/internal/mode/static/nginx/config/servers_test.go @@ -1310,8 +1310,8 @@ func TestCreateHeaderKeyValString(t *testing.T) { func TestIsPathOnlyMatch(t *testing.T) { tests := []struct { - match dataplane.Match msg string + match dataplane.Match expected bool }{ { From 67a789f6c06628f9bf01aac83d2fee9df0ea0917 Mon Sep 17 00:00:00 2001 From: Ciara Stacke Date: Mon, 21 Aug 2023 14:58:58 +0100 Subject: [PATCH 2/2] Fix G601 implicit memory aliasing in for loop errors --- internal/framework/status/httproute.go | 2 ++ internal/mode/static/state/dataplane/configuration.go | 5 ++++- internal/mode/static/state/graph/gateway_listener_test.go | 5 ++++- internal/mode/static/state/graph/secret.go | 4 +++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/framework/status/httproute.go b/internal/framework/status/httproute.go index 2d235a25b0..9b93e378aa 100644 --- a/internal/framework/status/httproute.go +++ b/internal/framework/status/httproute.go @@ -14,6 +14,8 @@ func prepareHTTPRouteStatus( parents := make([]v1beta1.RouteParentStatus, 0, len(status.ParentStatuses)) for _, ps := range status.ParentStatuses { + // reassign the iteration variable inside the loop to fix implicit memory aliasing + ps := ps p := v1beta1.RouteParentStatus{ ParentRef: v1beta1.ParentReference{ Namespace: (*v1beta1.Namespace)(&ps.GatewayNsName.Namespace), diff --git a/internal/mode/static/state/dataplane/configuration.go b/internal/mode/static/state/dataplane/configuration.go index 6cf8875371..cb41b52bd4 100644 --- a/internal/mode/static/state/dataplane/configuration.go +++ b/internal/mode/static/state/dataplane/configuration.go @@ -248,8 +248,11 @@ func (hpr *hostPathRules) upsertListener(l *graph.Listener) { rule.PathType = convertPathType(*m.Path.Type) } + // create iteration variable inside the loop to fix implicit memory aliasing + om := r.Source.ObjectMeta + rule.MatchRules = append(rule.MatchRules, MatchRule{ - Source: &r.Source.ObjectMeta, + Source: &om, BackendGroup: newBackendGroup(r.Rules[i].BackendRefs, routeNsName, i), Filters: filters, Match: convertMatch(m), diff --git a/internal/mode/static/state/graph/gateway_listener_test.go b/internal/mode/static/state/graph/gateway_listener_test.go index 191244047d..380a8363db 100644 --- a/internal/mode/static/state/graph/gateway_listener_test.go +++ b/internal/mode/static/state/graph/gateway_listener_test.go @@ -364,10 +364,13 @@ func TestValidateListenerLabelSelector(t *testing.T) { t.Run(test.name, func(t *testing.T) { g := NewGomegaWithT(t) + // create iteration variable inside the loop to fix implicit memory aliasing + from := test.from + listener := v1beta1.Listener{ AllowedRoutes: &v1beta1.AllowedRoutes{ Namespaces: &v1beta1.RouteNamespaces{ - From: &test.from, + From: &from, Selector: test.selector, }, }, diff --git a/internal/mode/static/state/graph/secret.go b/internal/mode/static/state/graph/secret.go index 19d90cf3b4..9cbbd84663 100644 --- a/internal/mode/static/state/graph/secret.go +++ b/internal/mode/static/state/graph/secret.go @@ -74,7 +74,9 @@ func (r *secretResolver) getResolvedSecrets() map[types.NamespacedName]*Secret { resolved := make(map[types.NamespacedName]*Secret) for nsname, entry := range r.resolvedSecrets { - resolved[nsname] = &entry.Secret + // create iteration variable inside the loop to fix implicit memory aliasing + secret := entry.Secret + resolved[nsname] = &secret } return resolved