Fresh pull from upstream
[librecmc/librecmc.git] / toolchain / musl / patches / 033-fix-undefined-behavior-in-sched.h-cpu_set_t-usage.patch
1 From 66570ec9c465e3c6c5d6dbd7dd42e45041a39288 Mon Sep 17 00:00:00 2001
2 From: Rich Felker <dalias@aerifal.cx>
3 Date: Mon, 19 Sep 2016 11:15:51 -0400
4 Subject: fix undefined behavior in sched.h cpu_set_t usage
5
6 since cpu sets can be dynamically allocated and have variable size,
7 accessing their contents via ->__bits is not valid; performing pointer
8 arithmetic outside the range of the size of the declared __bits array
9 results in undefined beahavior. instead, only use cpu_set_t for
10 fixed-size cpu set objects (instantiated by the caller) and as an
11 abstract pointer type for dynamically allocated ones. perform all
12 accesses simply by casting the abstract pointer type cpuset_t * back
13 to unsigned long *.
14 ---
15  include/sched.h | 6 +++---
16  1 file changed, 3 insertions(+), 3 deletions(-)
17
18 diff --git a/include/sched.h b/include/sched.h
19 index af82d6c..d1cccb7 100644
20 --- a/include/sched.h
21 +++ b/include/sched.h
22 @@ -82,7 +82,7 @@ int sched_getaffinity(pid_t, size_t, cpu_set_t *);
23  int sched_setaffinity(pid_t, size_t, const cpu_set_t *);
24  
25  #define __CPU_op_S(i, size, set, op) ( (i)/8U >= (size) ? 0 : \
26 -       ((set)->__bits[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )
27 +       (((unsigned long *)(set))[(i)/8/sizeof(long)] op (1UL<<((i)%(8*sizeof(long))))) )
28  
29  #define CPU_SET_S(i, size, set) __CPU_op_S(i, size, set, |=)
30  #define CPU_CLR_S(i, size, set) __CPU_op_S(i, size, set, &=~)
31 @@ -94,8 +94,8 @@ static __inline void __CPU_##func##_S(size_t __size, cpu_set_t *__dest, \
32  { \
33         size_t __i; \
34         for (__i=0; __i<__size/sizeof(long); __i++) \
35 -               __dest->__bits[__i] = __src1->__bits[__i] \
36 -                       op __src2->__bits[__i] ; \
37 +               ((unsigned long *)__dest)[__i] = ((unsigned long *)__src1)[__i] \
38 +                       op ((unsigned long *)__src2)[__i] ; \
39  }
40  
41  __CPU_op_func_S(AND, &)
42 -- 
43 cgit v0.11.2