From ca11026a032ce2a3de4b3c389ee53d2bdc8794d6 Mon Sep 17 00:00:00 2001 From: Paul Wankadia Date: Tue, 14 Jul 2020 10:23:46 -0700 Subject: [PATCH] Make Regexp::Simplify() return a null pointer when stopped early. Change-Id: I19e447e6c7dec34201299a12104e72bf7e792a9e Reviewed-on: https://code-review.googlesource.com/c/re2/+/57570 Reviewed-by: Paul Wankadia --- re2/prefilter.cc | 5 +++-- re2/simplify.cc | 14 +++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/re2/prefilter.cc b/re2/prefilter.cc index f61d54b8f..a47b3120f 100644 --- a/re2/prefilter.cc +++ b/re2/prefilter.cc @@ -648,14 +648,15 @@ Prefilter* Prefilter::FromRegexp(Regexp* re) { return NULL; Regexp* simple = re->Simplify(); - Prefilter::Info *info = BuildInfo(simple); + if (simple == NULL) + return NULL; + Prefilter::Info* info = BuildInfo(simple); simple->Decref(); if (info == NULL) return NULL; Prefilter* m = info->TakeMatch(); - delete info; return m; } diff --git a/re2/simplify.cc b/re2/simplify.cc index 270f0ff95..663d5fcd4 100644 --- a/re2/simplify.cc +++ b/re2/simplify.cc @@ -28,8 +28,6 @@ bool Regexp::SimplifyRegexp(const StringPiece& src, ParseFlags flags, Regexp* sre = re->Simplify(); re->Decref(); if (sre == NULL) { - // Should not happen, since Simplify never fails. - LOG(ERROR) << "Simplify failed on " << src; if (status) { status->set_code(kRegexpInternalError); status->set_error_arg(src); @@ -180,10 +178,20 @@ Regexp* Regexp::Simplify() { CoalesceWalker cw; Regexp* cre = cw.Walk(this, NULL); if (cre == NULL) - return cre; + return NULL; + if (cw.stopped_early()) { + cre->Decref(); + return NULL; + } SimplifyWalker sw; Regexp* sre = sw.Walk(cre, NULL); cre->Decref(); + if (sre == NULL) + return NULL; + if (sw.stopped_early()) { + sre->Decref(); + return NULL; + } return sre; }