getgroups test: Avoid warning with glibc >= 2.32 and gcc >= 10.
[gnulib.git] / tests / test-getgroups.c
blobdcea0332bb485e665cd259f99e4a92bcbd69cd53
1 /* Tests of getgroups.
2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 #include <config.h>
21 #include <unistd.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (getgroups, int, (int, gid_t[]));
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
31 #include "macros.h"
33 /* Tell GCC not to warn about the specific edge cases tested here.
34 GCC >= 10 with glibc >= 2.32 would otherwise trigger warnings, even without
35 any -W options, because getgroups() is declared with
36 __attribute__ ((__access__ (__write_only__, 2, 1)))
38 #if __GNUC__ >= 7
39 # pragma GCC diagnostic ignored "-Wstringop-overflow"
40 #endif
42 int
43 main (int argc, char **argv _GL_UNUSED)
45 int result;
46 gid_t *groups;
48 errno = 0;
49 result = getgroups (0, NULL);
50 if (result == -1 && errno == ENOSYS)
52 fputs ("skipping test: no support for groups\n", stderr);
53 return 77;
55 ASSERT (0 <= result);
56 ASSERT (result + 1 < SIZE_MAX / sizeof *groups);
57 groups = malloc ((result + 1) * sizeof *groups);
58 ASSERT (groups);
59 groups[result] = -1;
60 /* Check for EINVAL handling. Not all processes have supplemental
61 groups, and getgroups does not have to return the effective gid,
62 so a result of 0 is reasonable. Also, we can't test for EINVAL
63 if result is 1, because of how getgroups treats 0. */
64 if (1 < result)
66 errno = 0;
67 ASSERT (getgroups (result - 1, groups) == -1);
68 ASSERT (errno == EINVAL);
70 ASSERT (getgroups (result, groups) == result);
71 ASSERT (getgroups (result + 1, groups) == result);
72 ASSERT (groups[result] == -1);
73 errno = 0;
74 ASSERT (getgroups (-1, NULL) == -1);
75 ASSERT (errno == EINVAL);
77 /* The automated unit test, with no arguments, ends here. However,
78 for debugging purposes, you can pass a command-line argument to
79 list the returned groups. */
80 if (1 < argc)
82 int i;
83 for (i = 0; i < result; i++)
84 printf ("%d\n", (int) groups[i]);
86 free (groups);
87 return 0;