{
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;
#include "regex/regguts.h"
+#include "miscadmin.h" /* needed by rcancelrequested() */
+
/*
* forward declarations, up here so forward datatypes etc. are defined early
*/
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 *);
/* static function list */
static struct fns functions = {
rfree, /* regfree insides */
+ rcancelrequested /* check for cancel request */
};
}
}
+/*
+ * 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
/*
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 */
#include "catalog/pg_type.h"
#include "funcapi.h"
+#include "miscadmin.h"
#include "regex/regex.h"
#include "utils/builtins.h"
#include "utils/guc.h"
if (regcomp_result != REG_OKAY)
{
/* re didn't compile (no need for pg_regfree, if so) */
+
+ /*
+ * Here and in other places in this file, do CHECK_FOR_INTERRUPTS
+ * before reporting a regex error. This is so that if the regex
+ * library aborts and returns REG_CANCEL, we don't print an error
+ * message that implies the regex was invalid.
+ */
+ CHECK_FOR_INTERRUPTS();
+
pg_regerror(regcomp_result, &re_temp.cre_re, errMsg, sizeof(errMsg));
ereport(ERROR,
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
if (regexec_result != REG_OKAY && regexec_result != REG_NOMATCH)
{
/* re failed??? */
+ CHECK_FOR_INTERRUPTS();
pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
ereport(ERROR,
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
default:
/* re failed??? */
+ CHECK_FOR_INTERRUPTS();
pg_regerror(re_result, re, errMsg, sizeof(errMsg));
ereport(ERROR,
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
{
char errMsg[100];
+ CHECK_FOR_INTERRUPTS();
pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
ereport(ERROR,
(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
{
REG_ECOLORS, "REG_ECOLORS", "too many colors"
},
+
+{
+ REG_CANCEL, "REG_CANCEL", "operation cancelled"
+},
#define REG_BADOPT 18 /* invalid embedded option */
#define REG_ETOOBIG 19 /* nfa has too many states */
#define REG_ECOLORS 20 /* too many colors */
+#define REG_CANCEL 21 /* operation cancelled */
/* two specials for debugging and testing */
#define REG_ATOI 101 /* convert error-code name to number */
#define REG_ITOA 102 /* convert error-code number to name */
struct fns
{
void FUNCPTR(free, (regex_t *));
+ int FUNCPTR(cancel_requested, (void));
};
+#define CANCEL_REQUESTED(re) \
+ ((*((struct fns *) (re)->re_fns)->cancel_requested) ())
/*