From owner-svn-src-all@FreeBSD.ORG Sat Jun 20 20:44:28 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 78939106564A; Sat, 20 Jun 2009 20:44:28 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 66AA28FC0C; Sat, 20 Jun 2009 20:44:28 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n5KKiSPv086463; Sat, 20 Jun 2009 20:44:28 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n5KKiSY4086461; Sat, 20 Jun 2009 20:44:28 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <200906202044.n5KKiSY4086461@svn.freebsd.org> From: Jilles Tjoelker Date: Sat, 20 Jun 2009 20:44:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r194560 - head/bin/sh X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 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: Sat, 20 Jun 2009 20:44:28 -0000 Author: jilles Date: Sat Jun 20 20:44:27 2009 New Revision: 194560 URL: http://svn.freebsd.org/changeset/base/194560 Log: Fix race condition in noclobber option. Formerly, it was possible for the file to be created between the check if it existed and the open; the contents would then be lost. Because this must use O_EXCL, noclobber > will not create a file through a symlink anymore. This agrees with behaviour of other shells. Approved by: ed (mentor) (implicit) Modified: head/bin/sh/redir.c Modified: head/bin/sh/redir.c ============================================================================== --- head/bin/sh/redir.c Sat Jun 20 20:36:51 2009 (r194559) +++ head/bin/sh/redir.c Sat Jun 20 20:44:27 2009 (r194560) @@ -188,13 +188,25 @@ movefd: error("cannot create %s: %s", fname, strerror(errno)); goto movefd; case NTO: - fname = redir->nfile.expfname; - if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode)) - error("cannot create %s: %s", fname, - strerror(EEXIST)); - if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) - error("cannot create %s: %s", fname, strerror(errno)); - goto movefd; + if (Cflag) { + fname = redir->nfile.expfname; + if (stat(fname, &sb) == -1) { + if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) + error("cannot create %s: %s", fname, strerror(errno)); + } else if (!S_ISREG(sb.st_mode)) { + if ((f = open(fname, O_WRONLY, 0666)) < 0) + error("cannot create %s: %s", fname, strerror(errno)); + if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) { + close(f); + error("cannot create %s: %s", fname, + strerror(EEXIST)); + } + } else + error("cannot create %s: %s", fname, + strerror(EEXIST)); + goto movefd; + } + /* FALLTHROUGH */ case NCLOBBER: fname = redir->nfile.expfname; if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)