hust testsuite: fix a false positive
[oweals/busybox.git] / miscutils / taskset.c
index 8bd32ed61503f43841e30ef78c092a888bed7f03..fb352ab8dc5d64d8e314ea8d058182d5a59386b3 100644 (file)
@@ -75,29 +75,26 @@ static char *from_cpuset(cpu_set_t *mask)
 #define TASKSET_PRINTF_MASK "%llx"
 static unsigned long long from_cpuset(cpu_set_t *mask)
 {
-       struct BUG_CPU_SETSIZE_is_too_small {
-               char BUG_CPU_SETSIZE_is_too_small[
-                       CPU_SETSIZE < sizeof(int) ? -1 : 1];
-       };
-       char *p = (void*)mask;
-
-       /* Take the least significant bits. Careful!
-        * Consider both CPU_SETSIZE=4 and CPU_SETSIZE=1024 cases
+       BUILD_BUG_ON(CPU_SETSIZE < 8*sizeof(int));
+
+       /* Take the least significant bits. Assume cpu_set_t is
+        * implemented as an array of unsigned long or unsigned
+        * int.
         */
-#if BB_BIG_ENDIAN
-       /* For big endian, it means LAST bits */
-       if (CPU_SETSIZE < sizeof(long))
-               p += CPU_SETSIZE - sizeof(int);
-       else if (CPU_SETSIZE < sizeof(long long))
-               p += CPU_SETSIZE - sizeof(long);
-       else
-               p += CPU_SETSIZE - sizeof(long long);
-#endif
-       if (CPU_SETSIZE < sizeof(long))
-               return *(unsigned*)p;
-       if (CPU_SETSIZE < sizeof(long long))
-               return *(unsigned long*)p;
-       return *(unsigned long long*)p;
+       if (CPU_SETSIZE < 8*sizeof(long))
+               return *(unsigned*)mask;
+       if (CPU_SETSIZE < 8*sizeof(long long))
+               return *(unsigned long*)mask;
+# if BB_BIG_ENDIAN
+       if (sizeof(long long) > sizeof(long)) {
+               /* We can put two long in the long long, but they have to
+                * be swapped: the least significant word comes first in the
+                * array */
+               unsigned long *p = (void*)mask;
+               return p[0] + ((unsigned long long)p[1] << (8*sizeof(long)));
+       }
+# endif
+       return *(unsigned long long*)mask;
 }
 #endif
 
@@ -176,7 +173,7 @@ int taskset_main(int argc UNUSED_PARAM, char **argv)
                /* Cheap way to get "long enough" buffer */
                bin = xstrdup(aff);
 
-               if (aff[0] != '0' && (aff[1]|0x20) != 'x') {
+               if (aff[0] != '0' || (aff[1]|0x20) != 'x') {
 /* TODO: decimal/octal masks are still limited to 2^64 */
                        unsigned long long m = xstrtoull_range(aff, 0, 1, ULLONG_MAX);
                        bin += strlen(bin);