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

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