Blame nss/tst-nss-db-endpwent.c

Packit Service 727001
/* Test for endpwent->getpwent crash for BZ #24695
Packit Service 727001
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service 727001
   This file is part of the GNU C Library.
Packit Service 727001
Packit Service 727001
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 727001
   modify it under the terms of the GNU Lesser General Public
Packit Service 727001
   License as published by the Free Software Foundation; either
Packit Service 727001
   version 2.1 of the License, or (at your option) any later version.
Packit Service 727001
Packit Service 727001
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 727001
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 727001
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 727001
   Lesser General Public License for more details.
Packit Service 727001
Packit Service 727001
   You should have received a copy of the GNU Lesser General Public
Packit Service 727001
   License along with the GNU C Library; if not, see
Packit Service 727001
   <http://www.gnu.org/licenses/>.  */
Packit Service 727001
Packit Service 727001
#include <stdlib.h>
Packit Service 727001
#include <string.h>
Packit Service 727001
#include <sys/types.h>
Packit Service 727001
#include <pwd.h>
Packit Service 727001
Packit Service 727001
#include <support/support.h>
Packit Service 727001
#include <support/check.h>
Packit Service 727001
Packit Service 727001
/* It is entirely allowed to start with a getpwent call without
Packit Service 727001
   resetting the state of the service via a call to setpwent.
Packit Service 727001
   You can also call getpwent more times than you have entries in
Packit Service 727001
   the service, and it should not fail.  This test iteratates the
Packit Service 727001
   database once, gets to the end, and then attempts a second
Packit Service 727001
   iteration to look for crashes.  */
Packit Service 727001
Packit Service 727001
static void
Packit Service 727001
try_it (void)
Packit Service 727001
{
Packit Service 727001
  struct passwd *pw;
Packit Service 727001
Packit Service 727001
  /* setpwent is intentionally omitted here.  The first call to
Packit Service 727001
     getpwent detects that it's first and initializes.  The second
Packit Service 727001
     time try_it is called, this "first call" was not detected before
Packit Service 727001
     the fix, and getpwent would crash.  */
Packit Service 727001
Packit Service 727001
  while ((pw = getpwent ()) != NULL)
Packit Service 727001
    ;
Packit Service 727001
Packit Service 727001
  /* We only care if this segfaults or not.  */
Packit Service 727001
  endpwent ();
Packit Service 727001
}
Packit Service 727001
Packit Service 727001
static int
Packit Service 727001
do_test (void)
Packit Service 727001
{
Packit Service 727001
  char *cmd;
Packit Service 727001
Packit Service 727001
  cmd = xasprintf ("%s/makedb -o /var/db/passwd.db /var/db/passwd.in",
Packit Service 727001
		   support_bindir_prefix);
Packit Service 727001
  system (cmd);
Packit Service 727001
  free (cmd);
Packit Service 727001
Packit Service 727001
  try_it ();
Packit Service 727001
  try_it ();
Packit Service 727001
Packit Service 727001
  return 0;
Packit Service 727001
}
Packit Service 727001
#include <support/test-driver.c>