From owner-svn-src-head@FreeBSD.ORG Thu Jul 17 15:48:12 2014 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id B7900A2B; Thu, 17 Jul 2014 15:48:12 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::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 8B9B82408; Thu, 17 Jul 2014 15:48:12 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s6HFmC4a045272; Thu, 17 Jul 2014 15:48:12 GMT (envelope-from pfg@svn.freebsd.org) Received: (from pfg@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s6HFmCk5045268; Thu, 17 Jul 2014 15:48:12 GMT (envelope-from pfg@svn.freebsd.org) Message-Id: <201407171548.s6HFmCk5045268@svn.freebsd.org> From: "Pedro F. Giffuni" Date: Thu, 17 Jul 2014 15:48:12 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r268799 - head/usr.bin/grep 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.18 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, 17 Jul 2014 15:48:12 -0000 Author: pfg Date: Thu Jul 17 15:48:11 2014 New Revision: 268799 URL: http://svnweb.freebsd.org/changeset/base/268799 Log: grep: fix some memory leaks. Bring a couple of changes from NetBSD: queue.c (CVS Rev. 1.4. 1.5) Fix memory leaks. NULL does not need a cast. grep.c (CVS Rev. 1.6) Use the more portable getline. Obtained from: NetBSD MFC after: 3 days Modified: head/usr.bin/grep/grep.c head/usr.bin/grep/queue.c Modified: head/usr.bin/grep/grep.c ============================================================================== --- head/usr.bin/grep/grep.c Thu Jul 17 14:51:50 2014 (r268798) +++ head/usr.bin/grep/grep.c Thu Jul 17 15:48:11 2014 (r268799) @@ -1,4 +1,4 @@ -/* $NetBSD: grep.c,v 1.4 2011/02/16 01:31:33 joerg Exp $ */ +/* $NetBSD: grep.c,v 1.6 2011/04/18 03:48:23 joerg Exp $ */ /* $FreeBSD$ */ /* $OpenBSD: grep.c,v 1.42 2010/07/02 22:18:03 tedu Exp $ */ @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#define _WITH_GETLINE #include #include #include @@ -304,6 +305,7 @@ read_patterns(const char *fn) FILE *f; char *line; size_t len; + ssize_t rlen; if ((f = fopen(fn, "r")) == NULL) err(2, "%s", fn); @@ -311,8 +313,11 @@ read_patterns(const char *fn) fclose(f); return; } - while ((line = fgetln(f, &len)) != NULL) + len = 0; + line = NULL; + while ((rlen = getline(&line, &len, f)) != -1) add_pattern(line, line[0] == '\n' ? 0 : len); + free(line); if (ferror(f)) err(2, "%s", fn); fclose(f); Modified: head/usr.bin/grep/queue.c ============================================================================== --- head/usr.bin/grep/queue.c Thu Jul 17 14:51:50 2014 (r268798) +++ head/usr.bin/grep/queue.c Thu Jul 17 15:48:11 2014 (r268799) @@ -1,4 +1,4 @@ -/* $NetBSD: queue.c,v 1.2 2011/02/16 01:31:33 joerg Exp $ */ +/* $NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $ */ /* $FreeBSD$ */ /*- @@ -68,8 +68,11 @@ enqueue(struct str *x) STAILQ_INSERT_TAIL(&queue, item, list); - if (++count > Bflag) - free(dequeue()); + if (++count > Bflag) { + item = dequeue(); + free(item->data.dat); + free(item); + } } static struct qentry * @@ -92,7 +95,7 @@ printqueue(void) struct qentry *item; while ((item = dequeue()) != NULL) { - printline(&item->data, '-', (regmatch_t *)NULL, 0); + printline(&item->data, '-', NULL, 0); free(item); } } @@ -102,6 +105,8 @@ clearqueue(void) { struct qentry *item; - while ((item = dequeue()) != NULL) + while ((item = dequeue()) != NULL) { + free(item->data.dat); free(item); + } }