From owner-svn-src-head@freebsd.org Thu Jul 28 16:02:31 2016 Return-Path: Delivered-To: svn-src-head@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 CB201BA7758; Thu, 28 Jul 2016 16:02:31 +0000 (UTC) (envelope-from ed@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 7EE411278; Thu, 28 Jul 2016 16:02:31 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u6SG2Urp023729; Thu, 28 Jul 2016 16:02:30 GMT (envelope-from ed@FreeBSD.org) Received: (from ed@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u6SG2U3V023728; Thu, 28 Jul 2016 16:02:30 GMT (envelope-from ed@FreeBSD.org) Message-Id: <201607281602.u6SG2U3V023728@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ed set sender to ed@FreeBSD.org using -f From: Ed Schouten Date: Thu, 28 Jul 2016 16:02:30 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r303449 - head/usr.sbin/newsyslog X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jul 2016 16:02:31 -0000 Author: ed Date: Thu Jul 28 16:02:30 2016 New Revision: 303449 URL: https://svnweb.freebsd.org/changeset/base/303449 Log: Clean up use of basename() and dirname(). Pull copies of the input pathname string before calling basename() and dirname() to make this comply to POSIX. Free these copies at the end of this function. While there, remove the duplication of the 's' -> 'logfname' string. There is no need for this. Modified: head/usr.sbin/newsyslog/newsyslog.c Modified: head/usr.sbin/newsyslog/newsyslog.c ============================================================================== --- head/usr.sbin/newsyslog/newsyslog.c Thu Jul 28 15:57:01 2016 (r303448) +++ head/usr.sbin/newsyslog/newsyslog.c Thu Jul 28 16:02:30 2016 (r303449) @@ -2286,26 +2286,29 @@ mtime_old_timelog(const char *file) time_t t; struct dirent *dp; DIR *dirp; - char *s, *logfname, *dir; + char *logfname, *logfnamebuf, *dir, *dirbuf; t = -1; - if ((dir = dirname(file)) == NULL) { - warn("dirname() of '%s'", file); + if ((dirbuf = strdup(file)) == NULL) { + warn("strdup() of '%s'", file); return (t); } - if ((s = basename(file)) == NULL) { - warn("basename() of '%s'", file); + dir = dirname(dirbuf); + if ((logfnamebuf = strdup(file)) == NULL) { + warn("strdup() of '%s'", file); + free(dirbuf); return (t); - } else if (s[0] == '/') { - warnx("Invalid log filename '%s'", s); - return (t); - } else if ((logfname = strdup(s)) == NULL) - err(1, "strdup()"); + } + logfname = basename(logfnamebuf); + if (logfname[0] == '/') { + warnx("Invalid log filename '%s'", logfname); + goto out; + } if ((dirp = opendir(dir)) == NULL) { warn("Cannot open log directory '%s'", dir); - return (t); + goto out; } dir_fd = dirfd(dirp); /* Open the archive dir and find the most recent archive of logfname. */ @@ -2322,6 +2325,9 @@ mtime_old_timelog(const char *file) } closedir(dirp); +out: + free(dirbuf); + free(logfnamebuf); return (t); }