Skip to content

Commit 034cf19

Browse files
tjmoore4dsessler7
authored andcommitted
REL_5_8 Backpatch: Address prealloc lint issues
Does not include the update to internal/util/volumes.go due to differences in code. Issue: PGO-2844
1 parent be8d507 commit 034cf19

5 files changed

Lines changed: 29 additions & 17 deletions

File tree

internal/controller/postgrescluster/cluster.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,12 @@ func (r *Reconciler) reconcileDataSource(ctx context.Context,
403403
var configs []string
404404
switch {
405405
case dataSource != nil:
406-
configs = []string{dataSource.ClusterName, dataSource.RepoName}
406+
configs = make([]string, 0, 2+len(dataSource.Options))
407+
configs = append(configs, dataSource.ClusterName, dataSource.RepoName)
407408
configs = append(configs, dataSource.Options...)
408409
case cloudDataSource != nil:
409-
configs = []string{cloudDataSource.Stanza, cloudDataSource.Repo.Name}
410+
configs = make([]string, 0, 2+len(cloudDataSource.Options))
411+
configs = append(configs, cloudDataSource.Stanza, cloudDataSource.Repo.Name)
410412
configs = append(configs, cloudDataSource.Options...)
411413
}
412414
configHash, err := hashFunc(configs)

internal/controller/postgrescluster/instance.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,11 @@ func (r *Reconciler) scaleDownInstances(
978978
}
979979

980980
// grab all pods for the cluster using the observed instances
981-
pods := []corev1.Pod{}
981+
var podCount int
982+
for i := range observedInstances.forCluster {
983+
podCount += len(observedInstances.forCluster[i].Pods)
984+
}
985+
pods := make([]corev1.Pod, 0, podCount)
982986
for instanceIndex := range observedInstances.forCluster {
983987
for podIndex := range observedInstances.forCluster[instanceIndex].Pods {
984988
pods = append(pods, *observedInstances.forCluster[instanceIndex].Pods[podIndex])
@@ -1030,7 +1034,8 @@ func podsToKeep(instances []corev1.Pod, want map[string]int) []corev1.Pod {
10301034
return keep
10311035
}
10321036

1033-
keepPodList := []corev1.Pod{}
1037+
// preallocate with the total number of instance Pods
1038+
keepPodList := make([]corev1.Pod, 0, len(instances))
10341039
for name, num := range want {
10351040
list := []corev1.Pod{}
10361041
for _, instance := range instances {

internal/controller/standalone_pgadmin/configmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func generateClusterConfig(
199199
// which we can do by
200200
// a) sorting the ServerGroup name used as a key; and
201201
// b) sorting the clusters by name;
202-
keys := []string{}
202+
keys := make([]string, 0, len(clusters))
203203
for key := range clusters {
204204
keys = append(keys, key)
205205
}

internal/pgbouncer/config.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,20 @@ func podConfigFiles(
192192
// Start with an empty file at /etc/pgbouncer/pgbouncer.ini. This file can
193193
// be overridden by the user, but it must exist because our configuration
194194
// file refers to it.
195-
projections := []corev1.VolumeProjection{
196-
{
197-
ConfigMap: &corev1.ConfigMapProjection{
198-
LocalObjectReference: corev1.LocalObjectReference{
199-
Name: configmap.Name,
200-
},
201-
Items: []corev1.KeyToPath{{
202-
Key: emptyConfigMapKey,
203-
Path: emptyFileProjectionPath,
204-
}},
195+
// Preallocate: 3 (empty ini file + configmap projection + secret projection)
196+
// plus the number of specified files, len(config.Files)
197+
projections := make([]corev1.VolumeProjection, 0, 3+len(config.Files))
198+
projections = append(projections, corev1.VolumeProjection{
199+
ConfigMap: &corev1.ConfigMapProjection{
200+
LocalObjectReference: corev1.LocalObjectReference{
201+
Name: configmap.Name,
205202
},
203+
Items: []corev1.KeyToPath{{
204+
Key: emptyConfigMapKey,
205+
Path: emptyFileProjectionPath,
206+
}},
206207
},
207-
}
208+
})
208209

209210
// Add any specified projections. These may override the files above.
210211
// - https://docs.k8s.io/concepts/storage/volumes/#projected

internal/postgres/exec.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ func (exec Executor) ExecInDatabasesFromQuery(
7474
// database is passed via standard input while the database query is passed
7575
// as the first argument. Remaining arguments are passed through to `psql`.
7676
stdin := strings.NewReader(sql)
77-
args := []string{databases}
77+
78+
// Using make to preallocate args (using 1 (databases) + len(variables)) triggered
79+
// a CodeQL scanning error that a potentially large value might cause an overflow.
80+
// To address this, don't preallocate args and have the linter ignore this line.
81+
args := []string{databases} //nolint:prealloc
7882
for k, v := range variables {
7983
args = append(args, "--set="+k+"="+v)
8084
}

0 commit comments

Comments
 (0)