From owner-svn-src-all@freebsd.org Sun Sep 18 18:03:08 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 333FABE0D85; Sun, 18 Sep 2016 18:03:08 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DB8EA982; Sun, 18 Sep 2016 18:03:07 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u8II37QG035406; Sun, 18 Sep 2016 18:03:07 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u8II37mD035405; Sun, 18 Sep 2016 18:03:07 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201609181803.u8II37mD035405@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Sun, 18 Sep 2016 18:03:07 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r305945 - head/usr.bin/soelim X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 18 Sep 2016 18:03:08 -0000 Author: bapt Date: Sun Sep 18 18:03:06 2016 New Revision: 305945 URL: https://svnweb.freebsd.org/changeset/base/305945 Log: Better error checking if getcwd fails: just ignore it and do not try to adding to the list of possible path where to find the files. if fdopen fails, warn and return NULL the rest of the code knows how to deal with it Reported by: oshogbo Modified: head/usr.bin/soelim/soelim.c Modified: head/usr.bin/soelim/soelim.c ============================================================================== --- head/usr.bin/soelim/soelim.c Sun Sep 18 17:56:14 2016 (r305944) +++ head/usr.bin/soelim/soelim.c Sun Sep 18 18:03:06 2016 (r305945) @@ -68,6 +68,7 @@ relpath(const char *path) static FILE * soelim_fopen(int rootfd, const char *name) { + FILE *f = NULL; char path[PATH_MAX]; size_t i; int fd; @@ -75,8 +76,10 @@ soelim_fopen(int rootfd, const char *nam if (strcmp(name, "-") == 0) return (stdin); - if ((fd = openat(rootfd, relpath(name), O_RDONLY)) != -1) - return (fdopen(fd, "r")); + if ((fd = openat(rootfd, relpath(name), O_RDONLY)) != -1) { + f = fdopen(fd, "r"); + goto out; + } if (*name == '/') { warn("can't open '%s'", name); @@ -86,13 +89,17 @@ soelim_fopen(int rootfd, const char *nam for (i = 0; i < includes->sl_cur; i++) { snprintf(path, sizeof(path), "%s/%s", includes->sl_str[i], name); - if ((fd = openat(rootfd, relpath(path), O_RDONLY)) != -1) - return (fdopen(fd, "r")); + if ((fd = openat(rootfd, relpath(path), O_RDONLY)) != -1) { + f = fdopen(fd, "r"); + break; + } } - warn("can't open '%s'", name); +out: + if (f == NULL) + warn("can't open '%s'", name); - return (NULL); + return (f); } static int @@ -157,7 +164,9 @@ main(int argc, char **argv) cap_rights_t rights; includes = sl_init(); - sl_add(includes, getcwd(cwd, sizeof(cwd))); + if (getcwd(cwd, sizeof(cwd)) != NULL) + sl_add(includes, cwd); + if (includes == NULL) err(EXIT_FAILURE, "sl_init()"); @@ -196,6 +205,8 @@ main(int argc, char **argv) if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS) err(EXIT_FAILURE, "unable to limit rights for stderr"); rootfd = open("/", O_DIRECTORY | O_RDONLY); + if (rootfd == -1) + err(EXIT_FAILURE, "unable to open '/'"); cap_rights_init(&rights, CAP_READ, CAP_LOOKUP, CAP_FSTAT, CAP_FCNTL); if (cap_rights_limit(rootfd, &rights) < 0 && errno != ENOSYS) err(EXIT_FAILURE, "unable to limit rights");