提交 66570ec9 编写于 作者: R Rich Felker

fix undefined behavior in sched.h cpu_set_t usage

since cpu sets can be dynamically allocated and have variable size,
accessing their contents via ->__bits is not valid; performing pointer
arithmetic outside the range of the size of the declared __bits array
results in undefined beahavior. instead, only use cpu_set_t for
fixed-size cpu set objects (instantiated by the caller) and as an
abstract pointer type for dynamically allocated ones. perform all
accesses simply by casting the abstract pointer type cpuset_t * back
to unsigned long *.
上级 c002668e
...@@ -82,7 +82,7 @@ int sched_getaffinity(pid_t, size_t, cpu_set_t *); ...@@ -82,7 +82,7 @@ int sched_getaffinity(pid_t, size_t, cpu_set_t *);
int sched_setaffinity(pid_t, size_t, const cpu_set_t *); int sched_setaffinity(pid_t, size_t, const cpu_set_t *);
#define __CPU_op_S(i, size, set, op) ( (i)/8U >= (size) ? 0 : \ #define __CPU_op_S(i, size, set, op) ( (i)/8U >= (size) ? 0 : \
((set)->__bits[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) ) (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )
#define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=) #define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=)
#define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &=~) #define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &=~)
...@@ -94,8 +94,8 @@ static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, \ ...@@ -94,8 +94,8 @@ static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, \
{ \ { \
size_t __i; \ size_t __i; \
for (__i=0; __i<__size/sizeof(long); __i++) \ for (__i=0; __i<__size/sizeof(long); __i++) \
__dest->__bits[__i] = __src1->__bits[__i] \ ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] \
op __src2->__bits[__i] ; \ op ((unsigned long *)__src2)[__i] ; \
} }
__CPU_op_func_S(AND, &) __CPU_op_func_S(AND, &)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册