1 /* Test for endpwent->getpwent crash for BZ #24695
2    Copyright (C) 2019-2022 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 
27 /* It is entirely allowed to start with a getpwent call without
28    resetting the state of the service via a call to setpwent.
29    You can also call getpwent more times than you have entries in
30    the service, and it should not fail.  This test iteratates the
31    database once, gets to the end, and then attempts a second
32    iteration to look for crashes.  */
33 
34 static void
try_it(void)35 try_it (void)
36 {
37   struct passwd *pw;
38 
39   /* setpwent is intentionally omitted here.  The first call to
40      getpwent detects that it's first and initializes.  The second
41      time try_it is called, this "first call" was not detected before
42      the fix, and getpwent would crash.  */
43 
44   while ((pw = getpwent ()) != NULL)
45     ;
46 
47   /* We only care if this segfaults or not.  */
48   endpwent ();
49 }
50 
51 static int
do_test(void)52 do_test (void)
53 {
54   char *cmd;
55 
56   cmd = xasprintf ("%s/makedb -o /var/db/passwd.db /var/db/passwd.in",
57 		   support_bindir_prefix);
58   system (cmd);
59   free (cmd);
60 
61   try_it ();
62   try_it ();
63 
64   return 0;
65 }
66 #include <support/test-driver.c>
67