From 072391d5c55e887f1e2f0f87d6ee41b05d839f20 Mon Sep 17 00:00:00 2001 From: Maxim Muzafarov Date: Fri, 4 Oct 2024 12:05:26 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BFix=20conditions=20lexicographicLes?= =?UTF-8?q?s=20nil=20pointer=20dereference?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/conditions/setter.go | 10 ++++++++-- util/conditions/setter_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/util/conditions/setter.go b/util/conditions/setter.go index c387f0fc410c..dde10400f259 100644 --- a/util/conditions/setter.go +++ b/util/conditions/setter.go @@ -186,11 +186,17 @@ func Delete(to Setter, t clusterv1.ConditionType) { to.SetConditions(newConditions) } -// lexicographicLess returns true if a condition is less than another with regards to the -// to order of conditions designed for convenience of the consumer, i.e. kubectl. +// lexicographicLess returns true if a condition is less than another in regard to +// the order of conditions designed for convenience of the consumer, i.e. kubectl. // According to this order the Ready condition always goes first, followed by all the other // conditions sorted by Type. func lexicographicLess(i, j *clusterv1.Condition) bool { + if i == nil { + return true + } + if j == nil { + return false + } return (i.Type == clusterv1.ReadyCondition || i.Type < j.Type) && j.Type != clusterv1.ReadyCondition } diff --git a/util/conditions/setter_test.go b/util/conditions/setter_test.go index 57c3a770e629..8828ce9e13a2 100644 --- a/util/conditions/setter_test.go +++ b/util/conditions/setter_test.go @@ -84,6 +84,12 @@ func TestLexicographicLess(t *testing.T) { a = TrueCondition("A") b = TrueCondition(clusterv1.ReadyCondition) g.Expect(lexicographicLess(a, b)).To(BeFalse()) + + a = TrueCondition("A") + g.Expect(lexicographicLess(a, nil1)).To(BeFalse()) + + b = TrueCondition("A") + g.Expect(lexicographicLess(nil1, b)).To(BeTrue()) } func TestSet(t *testing.T) {