Skip to content

Commit 1965b91

Browse files
authored
Merge pull request #11254 from m-messiah/fix-has-same-state-nil
🐛Fix conditions hasSameState nil pointer dereference
2 parents e9be8cc + 44b9c11 commit 1965b91

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

util/conditions/setter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ func lexicographicLess(i, j *clusterv1.Condition) bool {
203203
// HasSameState returns true if a condition has the same state of another; state is defined
204204
// by the union of following fields: Type, Status, Reason, Severity and Message (it excludes LastTransitionTime).
205205
func HasSameState(i, j *clusterv1.Condition) bool {
206+
if i == nil || j == nil {
207+
return i == j
208+
}
206209
return i.Type == j.Type &&
207210
i.Status == j.Status &&
208211
i.Reason == j.Reason &&

util/conditions/setter_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ import (
3333
func TestHasSameState(t *testing.T) {
3434
g := NewWithT(t)
3535

36+
// two nils
37+
var nil2 *clusterv1.Condition
38+
g.Expect(HasSameState(nil1, nil2)).To(BeTrue())
39+
40+
// nil condition 1
41+
g.Expect(HasSameState(nil1, true1)).To(BeFalse())
42+
43+
// nil condition 2
44+
g.Expect(HasSameState(true1, nil2)).To(BeFalse())
45+
3646
// same condition
3747
falseInfo2 := falseInfo1.DeepCopy()
3848
g.Expect(HasSameState(falseInfo1, falseInfo2)).To(BeTrue())

0 commit comments

Comments
 (0)