1 | /* Test for endpwent->getpwent crash for BZ #24695 |
2 | Copyright (C) 2019-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <stdlib.h> |
20 | #include <string.h> |
21 | #include <sys/types.h> |
22 | #include <pwd.h> |
23 | |
24 | #include <support/support.h> |
25 | #include <support/check.h> |
26 | #include <support/xstdlib.h> |
27 | |
28 | /* It is entirely allowed to start with a getpwent call without |
29 | resetting the state of the service via a call to setpwent. |
30 | You can also call getpwent more times than you have entries in |
31 | the service, and it should not fail. This test iteratates the |
32 | database once, gets to the end, and then attempts a second |
33 | iteration to look for crashes. */ |
34 | |
35 | static void |
36 | try_it (void) |
37 | { |
38 | struct passwd *pw; |
39 | |
40 | /* setpwent is intentionally omitted here. The first call to |
41 | getpwent detects that it's first and initializes. The second |
42 | time try_it is called, this "first call" was not detected before |
43 | the fix, and getpwent would crash. */ |
44 | |
45 | while ((pw = getpwent ()) != NULL) |
46 | ; |
47 | |
48 | /* We only care if this segfaults or not. */ |
49 | endpwent (); |
50 | } |
51 | |
52 | static int |
53 | do_test (void) |
54 | { |
55 | char *cmd; |
56 | |
57 | cmd = xasprintf (format: "%s/makedb -o /var/db/passwd.db /var/db/passwd.in" , |
58 | support_bindir_prefix); |
59 | xsystem (cmd); |
60 | free (ptr: cmd); |
61 | |
62 | try_it (); |
63 | try_it (); |
64 | |
65 | return 0; |
66 | } |
67 | #include <support/test-driver.c> |
68 | |