aboutsummaryrefslogtreecommitdiff
path: root/src/backend/regex
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/regex')
-rw-r--r--src/backend/regex/regc_nfa.c12
-rw-r--r--src/backend/regex/regcomp.c20
-rw-r--r--src/backend/regex/regexec.c4
3 files changed, 36 insertions, 0 deletions
diff --git a/src/backend/regex/regc_nfa.c b/src/backend/regex/regc_nfa.c
index ae2dbe43fe8..f6dad013b54 100644
--- a/src/backend/regex/regc_nfa.c
+++ b/src/backend/regex/regc_nfa.c
@@ -174,11 +174,23 @@ newstate(struct nfa * nfa)
{
struct state *s;
+ /*
+ * This is a handy place to check for operation cancel during regex
+ * compilation, since no code path will go very long without making a new
+ * state.
+ */
+ if (CANCEL_REQUESTED(nfa->v->re))
+ {
+ NERR(REG_CANCEL);
+ return NULL;
+ }
+
if (TooManyStates(nfa))
{
NERR(REG_ETOOBIG);
return NULL;
}
+
if (nfa->free != NULL)
{
s = nfa->free;
diff --git a/src/backend/regex/regcomp.c b/src/backend/regex/regcomp.c
index ca1ccc52023..d31d7f7b727 100644
--- a/src/backend/regex/regcomp.c
+++ b/src/backend/regex/regcomp.c
@@ -34,6 +34,8 @@
#include "regex/regguts.h"
+#include "miscadmin.h" /* needed by rcancelrequested() */
+
/*
* forward declarations, up here so forward datatypes etc. are defined early
*/
@@ -67,6 +69,7 @@ static long nfanode(struct vars *, struct subre *, FILE *);
static int newlacon(struct vars *, struct state *, struct state *, int);
static void freelacons(struct subre *, int);
static void rfree(regex_t *);
+static int rcancelrequested(void);
#ifdef REG_DEBUG
static void dump(regex_t *, FILE *);
@@ -276,6 +279,7 @@ struct vars
/* static function list */
static const struct fns functions = {
rfree, /* regfree insides */
+ rcancelrequested /* check for cancel request */
};
@@ -1893,6 +1897,22 @@ rfree(regex_t *re)
}
}
+/*
+ * rcancelrequested - check for external request to cancel regex operation
+ *
+ * Return nonzero to fail the operation with error code REG_CANCEL,
+ * zero to keep going
+ *
+ * The current implementation is Postgres-specific. If we ever get around
+ * to splitting the regex code out as a standalone library, there will need
+ * to be some API to let applications define a callback function for this.
+ */
+static int
+rcancelrequested(void)
+{
+ return InterruptPending && (QueryCancelPending || ProcDiePending);
+}
+
#ifdef REG_DEBUG
/*
diff --git a/src/backend/regex/regexec.c b/src/backend/regex/regexec.c
index 78ebdee39e4..0edb83c1099 100644
--- a/src/backend/regex/regexec.c
+++ b/src/backend/regex/regexec.c
@@ -595,6 +595,10 @@ cdissect(struct vars * v,
assert(t != NULL);
MDEBUG(("cdissect %ld-%ld %c\n", LOFF(begin), LOFF(end), t->op));
+ /* handy place to check for operation cancel */
+ if (CANCEL_REQUESTED(v->re))
+ return REG_CANCEL;
+
switch (t->op)
{
case '=': /* terminal node */