Skip to content

Commit 6c0816b

Browse files
Merge pull request #30289 from bryan-cox/OCPBUGS-61398-b
OCPBUGS-61398: fix(ginkgo): avoid nil deref and harden env var setup
2 parents 4e3c58e + 2ff605c commit 6c0816b

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

pkg/test/ginkgo/status.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func (s *testSuiteProgress) TestEnded(testName string, testRunResult *testRunRes
4444
return
4545
}
4646

47+
if testRunResult.testRunResult == nil {
48+
fmt.Fprintln(os.Stderr, "testRunResult.testRunResult is nil")
49+
return
50+
}
51+
4752
if isTestFailed(testRunResult.testState) {
4853
s.failures++
4954
}

pkg/test/ginkgo/test_runner.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func (r *testSuiteRunnerImpl) RunOneTest(ctx context.Context, test *testCase) {
4141
// however, we can change the values of the content, so we assign the content lower down to have a cleaner set of
4242
// the many defers in this function.
4343
// remember that defers are last-added, first-executed.
44-
testRunResult := &testRunResultHandle{}
44+
// Initialize with a non-nil inner struct so deferred functions can safely read fields
45+
testRunResult := &testRunResultHandle{testRunResult: &testRunResult{testState: TestUnknown}}
4546

4647
// if we need to abort, then abort
4748
defer r.maybeAbortOnFailureFn(testRunResult)
@@ -225,6 +226,10 @@ func recordTestResultInLog(testRunResult *testRunResultHandle, out io.Writer, in
225226
fmt.Fprintln(os.Stderr, "testRunResult is nil")
226227
return
227228
}
229+
if testRunResult.testRunResult == nil {
230+
fmt.Fprintln(os.Stderr, "testRunResult.testRunResult is nil")
231+
return
232+
}
228233

229234
// output the status of the test
230235
switch testRunResult.testState {
@@ -267,6 +272,11 @@ func recordTestResultInMonitor(testRunResult *testRunResultHandle, monitorRecord
267272
return
268273
}
269274

275+
if testRunResult.testRunResult == nil {
276+
fmt.Fprintln(os.Stderr, "testRunResult.testRunResult is nil")
277+
return
278+
}
279+
270280
eventLevel := monitorapi.Warning
271281

272282
msg := monitorapi.NewMessage().HumanMessage("e2e test finished")
@@ -363,15 +373,21 @@ func updateEnvVars(envs []string) []string {
363373
// copied from provider.go
364374
// TODO: add error handling, and maybe turn this into sharable helper?
365375
config := &clusterdiscovery.ClusterConfiguration{}
366-
clientConfig, _ := framework.LoadConfig(true)
367-
clusterState, _ := clusterdiscovery.DiscoverClusterState(clientConfig)
368-
if clusterState != nil {
369-
config, _ = clusterdiscovery.LoadConfig(clusterState)
376+
if clientConfig, err := framework.LoadConfig(true); err == nil {
377+
if clusterState, err := clusterdiscovery.DiscoverClusterState(clientConfig); err == nil && clusterState != nil {
378+
if loaded, err := clusterdiscovery.LoadConfig(clusterState); err == nil && loaded != nil {
379+
config = loaded
380+
}
381+
}
370382
}
371383
if len(config.ProviderName) == 0 {
372384
config.ProviderName = "skeleton"
373385
}
374-
provider, _ := json.Marshal(config)
375-
result = append(result, fmt.Sprintf("TEST_PROVIDER=%s", provider))
386+
if provider, err := json.Marshal(config); err == nil {
387+
result = append(result, fmt.Sprintf("TEST_PROVIDER=%s", provider))
388+
} else {
389+
// fallback to a minimal default if marshal fails for any reason
390+
result = append(result, "TEST_PROVIDER=\"{\\\"ProviderName\\\":\\\"skeleton\\\"}\"")
391+
}
376392
return result
377393
}

0 commit comments

Comments
 (0)