From owner-svn-src-user@FreeBSD.ORG Wed Aug 31 23:04:25 2011 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 20245106564A; Wed, 31 Aug 2011 23:04:25 +0000 (UTC) (envelope-from gabor@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 0FEBD8FC0C; Wed, 31 Aug 2011 23:04:25 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p7VN4O4n020959; Wed, 31 Aug 2011 23:04:24 GMT (envelope-from gabor@svn.freebsd.org) Received: (from gabor@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p7VN4Osm020957; Wed, 31 Aug 2011 23:04:24 GMT (envelope-from gabor@svn.freebsd.org) Message-Id: <201108312304.p7VN4Osm020957@svn.freebsd.org> From: Gabor Kovesdan Date: Wed, 31 Aug 2011 23:04:24 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r225298 - user/gabor/grep/trunk X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 31 Aug 2011 23:04:25 -0000 Author: gabor Date: Wed Aug 31 23:04:24 2011 New Revision: 225298 URL: http://svn.freebsd.org/changeset/base/225298 Log: - Use fstat() after opening the file, which is safer - Only disallow directories for pattern file Suggested by: jh Modified: user/gabor/grep/trunk/grep.c Modified: user/gabor/grep/trunk/grep.c ============================================================================== --- user/gabor/grep/trunk/grep.c Wed Aug 31 16:54:05 2011 (r225297) +++ user/gabor/grep/trunk/grep.c Wed Aug 31 23:04:24 2011 (r225298) @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -293,13 +294,18 @@ read_patterns(const char *fn) FILE *f; char *line; size_t len; + int fd; - if ((stat(fn, &st) == -1) || !(S_ISREG(st.st_mode))) + if ((fd = open(fn, O_RDONLY)) == -1) return; - if ((f = fopen(fn, "r")) == NULL) + if ((fstat(fd, &st) == -1) || (S_ISDIR(st.st_mode))) { + close(fd); + return; + } + if ((f = fdopen(fd, "r")) == NULL) err(2, "%s", fn); - while ((line = fgetln(f, &len)) != NULL) - add_pattern(line, *line == '\n' ? 0 : len); + while ((line = fgetln(f, &len)) != NULL) + add_pattern(line, line[0] == '\n' ? 0 : len); if (ferror(f)) err(2, "%s", fn); fclose(f);