From 51e8f8bf760f7e72d92a3302945aa0b9f9e0b907 Mon Sep 17 00:00:00 2001 From: Sau P Date: Mon, 2 Dec 2024 22:16:07 +0000 Subject: [PATCH] fix(find.c): Check for empty pattern and free mem --- source/find.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/source/find.c b/source/find.c index 2cfc27e..0122300 100644 --- a/source/find.c +++ b/source/find.c @@ -672,6 +672,12 @@ char *findinclude(const char *pattern) { /* initialize */ int findinit(const char *pattern_) { + + if (pattern_ == NULL + || pattern_[0] == '\0') { + return NOTSYMBOL; + } + char *pattern = strdup(pattern_); int r = NOERROR; char buf[PATLEN + 3]; @@ -685,8 +691,11 @@ int findinit(const char *pattern_) { isregexp_valid = false; + /* Pattern length */ + size_t pattlen = strlen(pattern); + /* remove trailing white space */ - for(s = pattern + strlen(pattern) - 1; isspace((unsigned char)*s); --s) { + for(s = pattern + (pattlen - 1); isspace((unsigned char)*s); --s) { *s = '\0'; } @@ -772,6 +781,7 @@ int findinit(const char *pattern_) { } end: + free(pattern); return r; }