fix uninitialized output from sched_getaffinity
authorRich Felker <dalias@aerifal.cx>
Wed, 3 Dec 2014 02:54:36 +0000 (21:54 -0500)
committerRich Felker <dalias@aerifal.cx>
Wed, 3 Dec 2014 02:54:36 +0000 (21:54 -0500)
the sched_getaffinity syscall only fills a cpu set up to the set size
used/supported by the kernel. the rest is left untouched and userspace
is responsible for zero-filling it based on the return value of the
syscall.

src/sched/affinity.c

index 3b40211..737e41b 100644 (file)
@@ -1,5 +1,6 @@
 #define _GNU_SOURCE
 #include <sched.h>
+#include <string.h>
 #include "pthread_impl.h"
 #include "syscall.h"
 
@@ -16,7 +17,10 @@ int pthread_setaffinity_np(pthread_t td, size_t size, const cpu_set_t *set)
 int sched_getaffinity(pid_t tid, size_t size, cpu_set_t *set)
 {
        long ret = __syscall(SYS_sched_getaffinity, tid, size, set);
-       if (ret > 0) ret = 0;
+       if (ret > 0) {
+               if (ret < size) memset((char *)set+ret, 0, size-ret);
+               ret = 0;
+       }
        return __syscall_ret(ret);
 }