From 994a2b10363dab995109d172a8ee772616d2c901 Mon Sep 17 00:00:00 2001 From: Cedric Le Roux Date: Wed, 14 Feb 2024 08:14:11 -0800 Subject: [PATCH] client: fixed a bug where corrupt client state could panic the client (#19972) --- .changelog/19972.txt | 3 +++ client/client_test.go | 8 ++++++++ nomad/structs/structs.go | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 .changelog/19972.txt diff --git a/.changelog/19972.txt b/.changelog/19972.txt new file mode 100644 index 000000000..92c9a7fd2 --- /dev/null +++ b/.changelog/19972.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Fixed a bug where corrupt client state could panic the client +``` diff --git a/client/client_test.go b/client/client_test.go index 0fcb0c98a..d001dde32 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1876,6 +1876,14 @@ func TestClient_hasLocalState(t *testing.T) { c.stateDB = cstate.NewMemDB(c.logger) + t.Run("nil Job", func(t *testing.T) { + alloc := mock.BatchAlloc() + alloc.Job = nil + c.stateDB.PutAllocation(alloc) + + must.False(t, c.hasLocalState(alloc)) + }) + t.Run("plain alloc", func(t *testing.T) { alloc := mock.BatchAlloc() c.stateDB.PutAllocation(alloc) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index b64961fcb..971356050 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -4770,6 +4770,9 @@ func (j *Job) Warnings() error { // LookupTaskGroup finds a task group by name func (j *Job) LookupTaskGroup(name string) *TaskGroup { + if j == nil { + return nil + } for _, tg := range j.TaskGroups { if tg.Name == name { return tg