tst-nss-db-endpwent.c 2.0 KB

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