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

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