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

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