diff options
author | Yusuke Endoh <mame@ruby-lang.org> | 2023-11-07 04:11:35 +0900 |
---|---|---|
committer | Yusuke Endoh <mame@ruby-lang.org> | 2023-11-07 04:39:09 +0900 |
commit | 49b6dc8f07ec6baafa0881d8cb14029a66a04621 (patch) | |
tree | cba355a0deb1364b5b9012f1b1f89d5bef73531d /ext/socket | |
parent | deb6dd76e19273ad902857a0e4c3f37704326d09 (diff) |
Prevent cpu_set_t overflow even if there are more than 63 cores
Do not use `pthread_attr_setaffinity_np` if `sched_getcpu()` exceeds
`CPU_SETSIZE`. (Using `CPU_ALLOC()` would be more appropriate.)
Diffstat (limited to 'ext/socket')
-rw-r--r-- | ext/socket/raddrinfo.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index d959906a95..18585c2181 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -483,8 +483,11 @@ start: #if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU) cpu_set_t tmp_cpu_set; CPU_ZERO(&tmp_cpu_set); - CPU_SET(sched_getcpu(), &tmp_cpu_set); - pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set); + int cpu = sched_getcpu(); + if (cpu < CPU_SETSIZE) { + CPU_SET(cpu, &tmp_cpu_set); + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set); + } #endif pthread_t th; @@ -707,8 +710,11 @@ start: #if defined(HAVE_PTHREAD_ATTR_SETAFFINITY_NP) && defined(HAVE_SCHED_GETCPU) cpu_set_t tmp_cpu_set; CPU_ZERO(&tmp_cpu_set); - CPU_SET(sched_getcpu(), &tmp_cpu_set); - pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set); + int cpu = sched_getcpu(); + if (cpu < CPU_SETSIZE) { + CPU_SET(cpu, &tmp_cpu_set); + pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &tmp_cpu_set); + } #endif pthread_t th; |