From: Yu Watanabe Date: Fri, 25 Jul 2025 17:31:37 +0000 (+0900) Subject: test: add explicit test cases for cpu_set_add() and cpu_set_add_range() X-Git-Tag: v258-rc2~81^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ce3b1fa331f7c3e44354fe56a0ed92c33e54776;p=thirdparty%2Fsystemd.git test: add explicit test cases for cpu_set_add() and cpu_set_add_range() cpu_set_add_range() is used in parse_cpu_set(), hence already tested. But it is better to test these functions explicitly. For CID#1611787 and CID#1611788, that should be false-positive. --- diff --git a/src/shared/cpu-set-util.c b/src/shared/cpu-set-util.c index 222e4b7e179..7a155c12d41 100644 --- a/src/shared/cpu-set-util.c +++ b/src/shared/cpu-set-util.c @@ -179,7 +179,7 @@ int cpu_set_add_set(CPUSet *c, const CPUSet *src) { return 1; } -static int cpu_set_add_range(CPUSet *c, size_t start, size_t end) { +int cpu_set_add_range(CPUSet *c, size_t start, size_t end) { int r; assert(c); diff --git a/src/shared/cpu-set-util.h b/src/shared/cpu-set-util.h index 84a21809ca0..29582d54096 100644 --- a/src/shared/cpu-set-util.h +++ b/src/shared/cpu-set-util.h @@ -24,6 +24,7 @@ void cpu_set_done(CPUSet *c); int cpu_set_realloc(CPUSet *c, size_t n); int cpu_set_add(CPUSet *c, size_t i); int cpu_set_add_set(CPUSet *c, const CPUSet *src); +int cpu_set_add_range(CPUSet *c, size_t start, size_t end); int cpu_set_add_all(CPUSet *c); char* cpu_set_to_string(const CPUSet *c); diff --git a/src/test/test-cpu-set-util.c b/src/test/test-cpu-set-util.c index 6fc0ea0f498..eff64606101 100644 --- a/src/test/test-cpu-set-util.c +++ b/src/test/test-cpu-set-util.c @@ -262,4 +262,25 @@ TEST(print_cpu_alloc_size) { log_info("CPU_ALLOC_SIZE(8191) = %zu", CPU_ALLOC_SIZE(8191)); } +TEST(cpu_set_add) { + _cleanup_(cpu_set_done) CPUSet c = {}; + + for (size_t i = 0; i < 8192; i++) + ASSERT_OK(cpu_set_add(&c, 8191)); + + ASSERT_ERROR(cpu_set_add(&c, 8192), ERANGE); + ASSERT_ERROR(cpu_set_add(&c, SIZE_MAX), ERANGE); +} + +TEST(cpu_set_add_range) { + _cleanup_(cpu_set_done) CPUSet c = {}; + + ASSERT_ERROR(cpu_set_add_range(&c, 0, 8192), ERANGE); + ASSERT_ERROR(cpu_set_add_range(&c, 0, SIZE_MAX), ERANGE); + ASSERT_SIGNAL(cpu_set_add_range(&c, 100, 0), SIGABRT); + + ASSERT_OK(cpu_set_add_range(&c, 0, 0)); + ASSERT_OK(cpu_set_add_range(&c, 0, 8191)); +} + DEFINE_TEST_MAIN(LOG_DEBUG);