Skip to content

Commit

Permalink
Reject nodes with AST flags set for re_is_literal()
Browse files Browse the repository at this point in the history
One concession here is that I'm allowing `(?n)` because we don't currently indicate this in the AST.

I'm also explicitly permitting `RE_END_NL`, because by the API contract this is the caller's responsibility.
  • Loading branch information
katef committed Oct 30, 2023
1 parent de237ff commit 450d4f4
Show file tree
Hide file tree
Showing 25 changed files with 48 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/libre/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,31 @@ ast_expr_is_literal(const struct ast_expr *e,
*anchor_end = 1;
}

done:

for (i = 0; i < count; i++) {
if (nodes[i] == NULL || nodes[i]->type != AST_EXPR_LITERAL) {
return 0;
}
}

done:
/* mask out AST flags that we don't care about; these may be present or not */
int ast_flags = nodes[i]->flags
& ~(AST_FLAG_FIRST | AST_FLAG_LAST | AST_FLAG_ALWAYS_CONSUMES | AST_FLAG_CAN_CONSUME);

/* we reject anything else */
if (ast_flags != 0) {
return 0;
}

/* mask out re flags that we don't handle here yet (the caller is responsible for these) */
int re_flags = nodes[i]->re_flags
& ~(RE_END_NL);

/* and we don't permit any other re_flags */
if (re_flags != 0) {
return 0;
}
}

*n = count + is_end_nl;

Expand Down
1 change: 1 addition & 0 deletions tests/re_literal/in13.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
^(?i)a$
1 change: 1 addition & 0 deletions tests/re_literal/in14.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?:x(?i)a)
1 change: 1 addition & 0 deletions tests/re_literal/in15.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?i)
1 change: 1 addition & 0 deletions tests/re_literal/in16.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?i)xy
1 change: 1 addition & 0 deletions tests/re_literal/in17.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(?i)xy
1 change: 1 addition & 0 deletions tests/re_literal/in18.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(?:x(?i)xy)
1 change: 1 addition & 0 deletions tests/re_literal/in19.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(?:x(?-i)xy)
1 change: 1 addition & 0 deletions tests/re_literal/in20.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?i)(?:(?-i)a)(?:x(?-i)xy)
1 change: 1 addition & 0 deletions tests/re_literal/in21.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?i)(?:(?-i)a)(?:(?-i)x(?-i)xy)
1 change: 1 addition & 0 deletions tests/re_literal/in22.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?s)a
1 change: 1 addition & 0 deletions tests/re_literal/in23.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?x)a
1 change: 1 addition & 0 deletions tests/re_literal/in24.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(?n)a
1 change: 1 addition & 0 deletions tests/re_literal/out13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
1 change: 1 addition & 0 deletions tests/re_literal/out14.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
2 changes: 2 additions & 0 deletions tests/re_literal/out15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
anchors:
literal:
1 change: 1 addition & 0 deletions tests/re_literal/out16.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
1 change: 1 addition & 0 deletions tests/re_literal/out17.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
1 change: 1 addition & 0 deletions tests/re_literal/out18.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
2 changes: 2 additions & 0 deletions tests/re_literal/out19.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
anchors:
literal: axxy
1 change: 1 addition & 0 deletions tests/re_literal/out20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
2 changes: 2 additions & 0 deletions tests/re_literal/out21.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
anchors:
literal: axxy
1 change: 1 addition & 0 deletions tests/re_literal/out22.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
1 change: 1 addition & 0 deletions tests/re_literal/out23.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
non-literal
2 changes: 2 additions & 0 deletions tests/re_literal/out24.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
anchors:
literal: a

0 comments on commit 450d4f4

Please sign in to comment.