From 12b1ce6057668af1da1968599a9dffa4aeaaf3b9 Mon Sep 17 00:00:00 2001 From: matoval Date: Mon, 28 Apr 2025 12:21:00 -0700 Subject: [PATCH 1/4] Fix flock_test from interrupting utils package tests --- pkg/utils/flock_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go index 4755eead9..0b595bb93 100644 --- a/pkg/utils/flock_test.go +++ b/pkg/utils/flock_test.go @@ -57,6 +57,12 @@ func TestTryFLock(t *testing.T) { } func TestFLock_Unlock(t *testing.T) { + f, err := os.CreateTemp("", "flock-test") + if err != nil { + t.Error(err) + } + defer os.Remove(f.Name()) + defer f.Close() type fields struct { Fd int } @@ -68,7 +74,7 @@ func TestFLock_Unlock(t *testing.T) { { name: "Positive", fields: fields{ - Fd: 1, + Fd: int(f.Fd()), }, wantErr: false, }, From e101f1e4f50e125f039da882ebf6973bfc9c6cdb Mon Sep 17 00:00:00 2001 From: matoval Date: Mon, 28 Apr 2025 12:42:09 -0700 Subject: [PATCH 2/4] FIx integer overflow conversion uintptr -> int --- pkg/utils/flock_test.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go index 0b595bb93..ec1d9828b 100644 --- a/pkg/utils/flock_test.go +++ b/pkg/utils/flock_test.go @@ -6,6 +6,7 @@ package utils_test import ( "os" "path/filepath" + "strconv" "testing" "github.com/ansible/receptor/pkg/utils" @@ -62,7 +63,20 @@ func TestFLock_Unlock(t *testing.T) { t.Error(err) } defer os.Remove(f.Name()) - defer f.Close() + defer f.Close() + + var maxInt uintptr + if strconv.IntSize == 32 { + maxInt = uintptr(1<<31 - 1) + } else { + maxInt = uintptr(1<<63 - 1) + } + + fd := f.Fd() + if fd > maxInt { + t.Error(err) + } + type fields struct { Fd int } @@ -74,7 +88,7 @@ func TestFLock_Unlock(t *testing.T) { { name: "Positive", fields: fields{ - Fd: int(f.Fd()), + Fd: int(f.Fd()), // #nosec G115 }, wantErr: false, }, From 52497fae020b63fa3cf1b559bdd7d1fc20e89bd7 Mon Sep 17 00:00:00 2001 From: matoval Date: Mon, 28 Apr 2025 12:45:31 -0700 Subject: [PATCH 3/4] Lint --- pkg/utils/flock_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go index ec1d9828b..985383ca8 100644 --- a/pkg/utils/flock_test.go +++ b/pkg/utils/flock_test.go @@ -67,9 +67,9 @@ func TestFLock_Unlock(t *testing.T) { var maxInt uintptr if strconv.IntSize == 32 { - maxInt = uintptr(1<<31 - 1) + maxInt = uintptr(1<<31 - 1) } else { - maxInt = uintptr(1<<63 - 1) + maxInt = uintptr(1<<63 - 1) } fd := f.Fd() From 7035c6d76fa949b05cc98fbe8c0b194c7d622fc6 Mon Sep 17 00:00:00 2001 From: matoval Date: Mon, 28 Apr 2025 13:18:18 -0700 Subject: [PATCH 4/4] lint --- pkg/utils/flock_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/utils/flock_test.go b/pkg/utils/flock_test.go index 985383ca8..cb7894956 100644 --- a/pkg/utils/flock_test.go +++ b/pkg/utils/flock_test.go @@ -27,7 +27,7 @@ func TestTryFLock(t *testing.T) { args: args{ filename: filepath.Join(os.TempDir(), "good_flock_listener"), }, - want: &utils.FLock{0}, + want: &utils.FLock{Fd: 0}, wantErr: false, }, { @@ -63,7 +63,7 @@ func TestFLock_Unlock(t *testing.T) { t.Error(err) } defer os.Remove(f.Name()) - defer f.Close() + defer f.Close() var maxInt uintptr if strconv.IntSize == 32 {