Merge pull request #54366 from tamalsaha/patch-1

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Bug: Fix NPE in time.Equal method
This commit is contained in:
Kubernetes Submit Queue 2017-10-24 16:48:14 -07:00 committed by GitHub
commit 598d7e111e
2 changed files with 31 additions and 1 deletions

View File

@ -80,7 +80,13 @@ func (t *Time) Before(u *Time) bool {
// Equal reports whether the time instant t is equal to u.
func (t *Time) Equal(u *Time) bool {
return t.Time.Equal(u.Time)
if t == nil && u == nil {
return true
}
if t != nil && u != nil {
return t.Time.Equal(u.Time)
}
return false
}
// Unix returns the local time corresponding to the given Unix time

View File

@ -171,3 +171,27 @@ func TestTimeProto(t *testing.T) {
}
}
}
func TestTimeEqual(t *testing.T) {
t1 := NewTime(time.Now())
cases := []struct {
name string
x *Time
y *Time
result bool
}{
{"nil =? nil", nil, nil, true},
{"!nil =? !nil", &t1, &t1, true},
{"nil =? !nil", nil, &t1, false},
{"!nil =? nil", &t1, nil, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := c.x.Equal(c.y)
if result != c.result {
t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result)
}
})
}
}