git: 5b98bd30686a - main - sched tests: Avoid underflow when maxcpuid == 0

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Sun, 14 Jul 2024 18:30:15 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=5b98bd30686a358b2a20728a4a6b5cecb7e28dc9

commit 5b98bd30686a358b2a20728a4a6b5cecb7e28dc9
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-07-14 16:12:32 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-07-14 18:29:14 +0000

    sched tests: Avoid underflow when maxcpuid == 0
    
    MFC after:      1 week
---
 tests/sys/kern/sched_affinity.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/tests/sys/kern/sched_affinity.c b/tests/sys/kern/sched_affinity.c
index cbba226b3fa2..9af6076c1649 100644
--- a/tests/sys/kern/sched_affinity.c
+++ b/tests/sys/kern/sched_affinity.c
@@ -67,13 +67,16 @@ ATF_TC_BODY(test_setinvalidcpu, tc)
 {
 	size_t cpusetsize;
 	cpuset_t *set;
+	int cpu;
+
+	cpu = maxcpuid > 1 ? maxcpuid - 1 : 0;
 
 	cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1);
 	set = CPU_ALLOC(maxcpuid + 1);
 	ATF_REQUIRE(set != NULL);
 	CPU_ZERO_S(cpusetsize, set);
 	CPU_SET_S(maxcpuid + 1, cpusetsize, set);
-	CPU_SET_S(maxcpuid - 1, cpusetsize, set);
+	CPU_SET_S(cpu, cpusetsize, set);
 	ATF_REQUIRE(sched_setaffinity(0, cpusetsize, set) == 0);
 	CPU_FREE(set);
 
@@ -82,7 +85,7 @@ ATF_TC_BODY(test_setinvalidcpu, tc)
 	ATF_REQUIRE(set != NULL);
 	CPU_ZERO_S(cpusetsize, set);
 	CPU_SET_S(maxcpuid + 1, cpusetsize, set);
-	CPU_SET_S(maxcpuid - 1, cpusetsize, set);
+	CPU_SET_S(cpu, cpusetsize, set);
 	ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
 	    -1, cpusetsize, set) == -1);
 	ATF_REQUIRE_EQ(errno, EINVAL);