From 6293358d19687aba69f82eb7e27e50e7dc7fb840 Mon Sep 17 00:00:00 2001 From: cleroux Date: Tue, 13 Feb 2024 17:37:20 -0800 Subject: [PATCH 1/3] Fix nil pointer dereference if alloc has nil Job --- client/client_test.go | 8 ++++++++ nomad/structs/structs.go | 3 +++ 2 files changed, 11 insertions(+) diff --git a/client/client_test.go b/client/client_test.go index 0fcb0c98a32..bcb750900fb 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) + + require.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 b64961fcb6f..9713560503a 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 From 9643fd03db4731292a263bf44b8c8d514141ee70 Mon Sep 17 00:00:00 2001 From: cleroux Date: Wed, 14 Feb 2024 07:09:08 -0800 Subject: [PATCH 2/3] Add changelog --- .changelog/19972.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/19972.txt diff --git a/.changelog/19972.txt b/.changelog/19972.txt new file mode 100644 index 00000000000..92c9a7fd2dc --- /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 +``` From 5258f2be62450f9eeb738f363bea8aaf3f4b81ae Mon Sep 17 00:00:00 2001 From: Cedric Le Roux Date: Wed, 14 Feb 2024 07:12:06 -0800 Subject: [PATCH 3/3] Use shoenig/test instead of testify Co-authored-by: Tim Gross --- client/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/client_test.go b/client/client_test.go index bcb750900fb..d001dde32e9 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1881,7 +1881,7 @@ func TestClient_hasLocalState(t *testing.T) { alloc.Job = nil c.stateDB.PutAllocation(alloc) - require.False(t, c.hasLocalState(alloc)) + must.False(t, c.hasLocalState(alloc)) }) t.Run("plain alloc", func(t *testing.T) {