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

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