1/* Make sure that pthread_attr_getaffinity_np does not crash when the input
2 cpuset size is smaller than that in the attribute structure.
3
4 Copyright (C) 2013-2022 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
20
21#include <pthread.h>
22#include <stdio.h>
23#include <sched.h>
24#include <errno.h>
25#include <sys/param.h>
26
27
28#define RETURN_IF_FAIL(f, ...) \
29 ({ \
30 int ret = f (__VA_ARGS__); \
31 if (ret != 0) \
32 { \
33 printf ("%s:%d: %s returned %d (errno = %d)\n", __FILE__, __LINE__, \
34 #f, ret, errno); \
35 return ret; \
36 } \
37 })
38
39static int
40do_test (void)
41{
42 for (int i = 0; i < 10; i++)
43 {
44 pthread_attr_t attr;
45 cpu_set_t *cpuset = CPU_ALLOC (512);
46 size_t cpusetsize = CPU_ALLOC_SIZE (512);
47 CPU_ZERO_S (cpusetsize, cpuset);
48
49 RETURN_IF_FAIL (pthread_attr_init, &attr);
50 RETURN_IF_FAIL (pthread_attr_setaffinity_np, &attr, cpusetsize, cpuset);
51 CPU_FREE (cpuset);
52
53 cpuset = CPU_ALLOC (1);
54 cpusetsize = CPU_ALLOC_SIZE (1);
55 RETURN_IF_FAIL (pthread_attr_getaffinity_np, &attr, cpusetsize, cpuset);
56 CPU_FREE (cpuset);
57 }
58 return 0;
59}
60
61
62#define TEST_FUNCTION do_test ()
63#include "../test-skeleton.c"
64

source code of glibc/nptl/tst-pthread-attr-affinity.c