From a43b7a916e84a9e741be887aad51cd9c7fea1591 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Thu, 18 Dec 2008 09:30:06 +0100 Subject: [PATCH] handle NULL pointer in regExp --- applications/test/regex/Make/options | 3 --- applications/test/regex/regexTest.C | 30 ++++++++++++++++++++++++++++ src/OSspecific/Unix/regExp.C | 28 ++++++++++++++++++-------- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/applications/test/regex/Make/options b/applications/test/regex/Make/options index 0eb7b642835..e69de29bb2d 100644 --- a/applications/test/regex/Make/options +++ b/applications/test/regex/Make/options @@ -1,3 +0,0 @@ -EXE_LIBS = \ - $(FOAM_LIBBIN)/libOSspecific.o - diff --git a/applications/test/regex/regexTest.C b/applications/test/regex/regexTest.C index 51c07b9ceec..0463891dc1e 100644 --- a/applications/test/regex/regexTest.C +++ b/applications/test/regex/regexTest.C @@ -76,6 +76,36 @@ int main(int argc, char *argv[]) Info << endl; } + Info<<"test regExp(const char*) ..." << endl; + string me("Mark"); + + if (regExp("[Mm]ar[ck]").match(me)) + { + Info<< "matched: " << me << endl; + } + else + { + Info<< "no match" << endl; + } + + if (regExp("").match(me)) + { + Info<< "matched: " << me << endl; + } + else + { + Info<< "no match" << endl; + } + + if (regExp(NULL).match(me)) + { + Info<< "matched: " << me << endl; + } + else + { + Info<< "no match" << endl; + } + Info<< endl; return 0; diff --git a/src/OSspecific/Unix/regExp.C b/src/OSspecific/Unix/regExp.C index 62c69512b71..e3ad4a11af4 100644 --- a/src/OSspecific/Unix/regExp.C +++ b/src/OSspecific/Unix/regExp.C @@ -37,15 +37,20 @@ License void Foam::regExp::compile(const char* pat) const { clear(); - preg_ = new regex_t; - if (regcomp(preg_, pat, REG_EXTENDED) != 0) + // avoid NULL and zero-length patterns + if (pat && *pat) { - FatalErrorIn - ( - "regExp::compile(const char*)" - ) << "Failed to compile regular expression '" << pat << "'" - << exit(FatalError); + preg_ = new regex_t; + + if (regcomp(preg_, pat, REG_EXTENDED) != 0) + { + FatalErrorIn + ( + "regExp::compile(const char*)" + ) << "Failed to compile regular expression '" << pat << "'" + << exit(FatalError); + } } } @@ -60,6 +65,7 @@ void Foam::regExp::clear() const } } + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::regExp::regExp() @@ -83,6 +89,7 @@ Foam::regExp::regExp(const char* pat) compile(pat); } + // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::regExp::~regExp() @@ -90,6 +97,7 @@ Foam::regExp::~regExp() clear(); } + // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // int Foam::regExp::ngroups() const @@ -110,6 +118,7 @@ bool Foam::regExp::match regmatch_t pmatch[1]; // match and also verify that the entire string was matched + // pmatch[0] is the entire match if ( regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0 @@ -141,6 +150,8 @@ bool Foam::regExp::match regmatch_t pmatch[nmatch]; // match and also verify that the entire string was matched + // pmatch[0] is the entire match + // pmatch[1..] are the (...) sub-groups if ( regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0 @@ -179,8 +190,8 @@ bool Foam::regExp::match return false; } -// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // void Foam::regExp::operator=(const string& pat) { @@ -193,4 +204,5 @@ void Foam::regExp::operator=(const char* pat) compile(pat); } + // ************************************************************************* // -- GitLab