From aad913db60448b36793b47f03c02a1eb089f9158 Mon Sep 17 00:00:00 2001 From: Paul Mars Date: Tue, 19 Sep 2023 16:54:33 +0200 Subject: [PATCH] Avoid implicit memory aliasing in for loop --- internal/statemachine/classic_test.go | 12 ++++++------ internal/statemachine/common_states.go | 2 +- internal/statemachine/helper_test.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/statemachine/classic_test.go b/internal/statemachine/classic_test.go index 1483b162..ee09d2fa 100644 --- a/internal/statemachine/classic_test.go +++ b/internal/statemachine/classic_test.go @@ -1035,7 +1035,7 @@ func TestCustomizeCloudInit(t *testing.T) { }, } - for _, cloudInitConfig := range cloudInitConfigs { + for i, cloudInitConfig := range cloudInitConfigs { t.Run("test_customize_cloud_init", func(t *testing.T) { // Test setup asserter := helper.Asserter{T: t} @@ -1049,11 +1049,11 @@ func TestCustomizeCloudInit(t *testing.T) { asserter.AssertErrNil(err, true) defer func() { if tmpErr := osRemoveAll(tmpDir); tmpErr != nil { - if err != nil { + if err != nil { err = fmt.Errorf("%w after previous error: %w", tmpErr, err) } else { err = tmpErr - } + } } }() stateMachine.tempDirs.chroot = tmpDir @@ -1063,7 +1063,7 @@ func TestCustomizeCloudInit(t *testing.T) { asserter.AssertErrNil(err, true) stateMachine.ImageDef.Customization = &imagedefinition.Customization{ - CloudInit: &cloudInitConfig, + CloudInit: &cloudInitConfigs[i], } // Running function to test @@ -2158,12 +2158,12 @@ func TestCheckEmptyFields(t *testing.T) { {"missing_implicitly_required", testStruct{A: "foo", C: "baz"}, false}, {"missing_omitempty", testStruct{A: "foo", B: "bar"}, true}, } - for _, tc := range testCases { + for i, tc := range testCases { t.Run("test_check_empty_fields_"+tc.name, func(t *testing.T) { asserter := helper.Asserter{T: t} result := new(gojsonschema.Result) - err := helper.CheckEmptyFields(&tc.structData, result, schema) + err := helper.CheckEmptyFields(&testCases[i].structData, result, schema) asserter.AssertErrNil(err, false) schema.Required = append(schema.Required, "fieldA") diff --git a/internal/statemachine/common_states.go b/internal/statemachine/common_states.go index b3474fb1..2b7a1015 100644 --- a/internal/statemachine/common_states.go +++ b/internal/statemachine/common_states.go @@ -268,7 +268,7 @@ func (stateMachine *StateMachine) populateBootfsContents() error { } } if laidOutStructure.HasFilesystem() { - mountedFilesystemWriter, err := gadgetNewMountedFilesystemWriter(&laidOutStructure, nil) + mountedFilesystemWriter, err := gadgetNewMountedFilesystemWriter(&laidOutVolume.LaidOutStructure[ii], nil) if err != nil { return fmt.Errorf("Error creating NewMountedFilesystemWriter: %s", err.Error()) } diff --git a/internal/statemachine/helper_test.go b/internal/statemachine/helper_test.go index e726c43a..80580901 100644 --- a/internal/statemachine/helper_test.go +++ b/internal/statemachine/helper_test.go @@ -532,7 +532,7 @@ func TestGenerateUniqueDiskID(t *testing.T) { {"collision", [][]byte{{0, 1, 2, 3}}, [][]byte{{0, 1, 2, 3}, {4, 5, 6, 7}}, []byte{4, 5, 6, 7}, false}, {"broken", [][]byte{{0, 0, 0, 0}}, nil, []byte{0, 0, 0, 0}, true}, } - for _, tc := range testCases { + for i, tc := range testCases { t.Run("test_generate_unique_diskid_"+tc.name, func(t *testing.T) { asserter := helper.Asserter{T: t} // create a test rng reader, using data from our testcase @@ -552,7 +552,7 @@ func TestGenerateUniqueDiskID(t *testing.T) { randRead = rand.Read }() - randomBytes, err := generateUniqueDiskID(&tc.existing) + randomBytes, err := generateUniqueDiskID(&testCases[i].existing) if tc.expectedErr { asserter.AssertErrContains(err, "Failed to generate unique disk ID") } else {