From e12129b1779d28fd83cd324f4db1fea464a9c22b Mon Sep 17 00:00:00 2001 From: pfg Date: Mon, 21 Jul 2014 23:00:26 +0000 Subject: [PATCH] MFC r268798, r268799, r268801: grep: Fix type. grep: fix some memory leaks. 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 git-svn-id: svn://svn.freebsd.org/base/stable/9@268967 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- usr.bin/grep/grep.c | 9 +++++++-- usr.bin/grep/queue.c | 16 +++++++++++----- usr.bin/grep/util.c | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 52b8dc9fd..5ca1bc9bc 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -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 @@ -303,6 +304,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); @@ -310,8 +312,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); diff --git a/usr.bin/grep/queue.c b/usr.bin/grep/queue.c index afcb82701..18878880a 100644 --- a/usr.bin/grep/queue.c +++ b/usr.bin/grep/queue.c @@ -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,8 @@ printqueue(void) struct qentry *item; while ((item = dequeue()) != NULL) { - printline(&item->data, '-', (regmatch_t *)NULL, 0); + printline(&item->data, '-', NULL, 0); + free(item->data.dat); free(item); } } @@ -102,6 +106,8 @@ clearqueue(void) { struct qentry *item; - while ((item = dequeue()) != NULL) + while ((item = dequeue()) != NULL) { + free(item->data.dat); free(item); + } } diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 15b2da74e..3ec12fa75 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -302,7 +302,7 @@ procline(struct str *l, int nottext) r = REG_NOMATCH; /* Check for whole word match */ if (r == 0 && (wflag || fg_pattern[i].word)) { - wint_t wbegin, wend; + wchar_t wbegin, wend; wbegin = wend = L' '; if (pmatch.rm_so != 0 && -- 2.45.0