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

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