From 33f5799a815541bbf0c754920d695d9b27ef491d Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Thu, 28 Jul 2016 15:19:47 +0000 Subject: [PATCH] Call basename() in a portable way. Pull a copy of the filename string before calling basename(). Change the loop to not return on its own, so we can put a free() statement at the bottom. --- usr.bin/grep/util.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index f3cf05f04119..5583c803abcc 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -58,21 +58,26 @@ static int procline(struct str *l, int); bool file_matching(const char *fname) { - char *fname_base; + char *fname_base, *fname_buf; bool ret; ret = finclude ? false : true; - fname_base = basename(fname); + fname_buf = strdup(fname); + if (fname_buf == NULL) + err(2, "strdup"); + fname_base = basename(fname_buf); for (unsigned int i = 0; i < fpatterns; ++i) { if (fnmatch(fpattern[i].pat, fname, 0) == 0 || fnmatch(fpattern[i].pat, fname_base, 0) == 0) { - if (fpattern[i].mode == EXCL_PAT) - return (false); - else + if (fpattern[i].mode == EXCL_PAT) { + ret = false; + break; + } else ret = true; } } + free(fname_buf); return (ret); }