/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Bad calls to fstat, lstat, and stat31*/3233#include <sys/types.h>34#include <sys/stat.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>39#include <errno.h>40#include <err.h>4142#include "config.h"43#include "test.h"4445////////////////////////////////////////////////////////////4647static48int49badbuf_fstat(struct stat *sb)50{51return fstat(STDIN_FILENO, sb);52}5354static55int56badbuf_lstat(struct stat *sb)57{58return lstat("null:", sb);59}6061static62int63badbuf_stat(struct stat *sb)64{65return stat("null:", sb);66}6768static69void70common_badbuf(int (*statfunc)(struct stat *), void *ptr,71const char *call, const char *ptrdesc)72{73int rv;74char mydesc[128];7576snprintf(mydesc, sizeof(mydesc), "%s with %s buf", call, ptrdesc);77rv = statfunc(ptr);78report_test(rv, errno, EFAULT, mydesc);79}8081static82void83any_badbuf(int (*statfunc)(struct stat *), const char *call)84{85common_badbuf(statfunc, NULL, call, "NULL");86common_badbuf(statfunc, INVAL_PTR, call, "invalid pointer");87common_badbuf(statfunc, KERN_PTR, call, "kernel pointer");88}8990////////////////////////////////////////////////////////////9192static93void94any_empty(int (*statfunc)(const char *, struct stat *), const char *call)95{96struct stat sb;97char desc[128];98int rv;99100snprintf(desc, sizeof(desc), "%s on empty string", call);101rv = statfunc("", &sb);102report_test2(rv, errno, 0, EINVAL, desc);103}104105////////////////////////////////////////////////////////////106107void108test_fstat(void)109{110test_fstat_fd();111any_badbuf(badbuf_fstat, "fstat");112}113114void115test_lstat(void)116{117test_lstat_path();118any_empty(lstat, "stat");119any_badbuf(badbuf_lstat, "lstat");120}121122void123test_stat(void)124{125test_stat_path();126any_empty(stat, "stat");127any_badbuf(badbuf_stat, "stat");128}129130131132