summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/regex/regcomp.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c
index fb24556e..b4b81968 100644
--- a/src/regex/regcomp.c
+++ b/src/regex/regcomp.c
@@ -409,6 +409,8 @@ typedef struct {
int position;
/* The highest back reference or -1 if none seen so far. */
int max_backref;
+ /* Bit mask of submatch IDs that can be back referenced. */
+ int backref_ok;
/* Compilation flags. */
int cflags;
} tre_parse_ctx_t;
@@ -769,6 +771,8 @@ static reg_errcode_t marksub(tre_parse_ctx_t *ctx, tre_ast_node_t *node, int sub
node->submatch_id = subid;
node->num_submatches++;
ctx->n = node;
+ if (subid < 10)
+ ctx->backref_ok |= 1<<subid;
return REG_OK;
}
@@ -864,6 +868,8 @@ static reg_errcode_t parse_atom(tre_parse_ctx_t *ctx, const char *s)
if (!ere && (unsigned)*s-'1' < 9) {
/* back reference */
int val = *s - '0';
+ if (!(ctx->backref_ok & 1<<val))
+ return REG_ESUBREG;
node = tre_ast_new_literal(ctx->mem, BACKREF, val, ctx->position++);
ctx->max_backref = MAX(val, ctx->max_backref);
} else {