Skip to content

Commit 81783e6

Browse files
Copilotjketema
authored andcommitted
C++: Implement \0 NUL escape
1 parent 87cdedd commit 81783e6

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

‎cpp/ql/lib/semmle/code/cpp/regex/internal/ParseRegExp.qll‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ abstract class RegExp extends StringLiteral {
325325
// control char \cX
326326
this.getChar(start + 1) = "c" and end = start + 3
327327
or
328+
// nul escape \0 (when not followed by a digit)
329+
this.getChar(start + 1) = "0" and
330+
end = start + 2 and
331+
not exists(this.getChar(start + 2).toInt())
332+
or
328333
// escape not handled above; update when adding a new case
329334
not this.getChar(start + 1) in ["x", "u", "c"] and
330335
not exists(this.getChar(start + 1).toInt()) and
@@ -598,9 +603,7 @@ abstract class RegExp extends StringLiteral {
598603
}
599604

600605
/** Whether the text in the range `start,end` is a back reference */
601-
predicate backreference(int start, int end) {
602-
this.numberedBackreference(start, end, _)
603-
}
606+
predicate backreference(int start, int end) { this.numberedBackreference(start, end, _) }
604607

605608
/** Gets the number of the back reference in start,end */
606609
int getBackRefNumber(int start, int end) { this.numberedBackreference(start, end, result) }

‎cpp/ql/test/library-tests/regex/parse.expected‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,12 @@ regexp.cpp:
408408

409409
# 85| [RegExpConstant, RegExpEscape] \cZ
410410

411-
# 88| [RegExpConstant, RegExpNormalChar] \0
411+
# 88| [RegExpConstant, RegExpEscape] \0
412412

413413
# 89| [RegExpCharacterClass] [\0]
414-
#-----| 0 -> [RegExpConstant, RegExpNormalChar] \
415-
#-----| 1 -> [RegExpConstant, RegExpNormalChar] 0
414+
#-----| 0 -> [RegExpConstant, RegExpEscape] \0
416415

417-
# 89| [RegExpConstant, RegExpNormalChar] \
418-
419-
# 89| [RegExpConstant, RegExpNormalChar] 0
416+
# 89| [RegExpConstant, RegExpEscape] \0
420417

421418
# 92| [RegExpConstant, RegExpNormalChar] a
422419

‎cpp/ql/test/library-tests/regex/regexp.expected‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ term
153153
| regexp.cpp:84:21:84:23 | \\cA | RegExpConstant,RegExpEscape |
154154
| regexp.cpp:85:21:85:25 | [\\cZ] | RegExpCharacterClass |
155155
| regexp.cpp:85:22:85:24 | \\cZ | RegExpConstant,RegExpEscape |
156-
| regexp.cpp:88:20:88:21 | \\0 | RegExpConstant,RegExpNormalChar |
156+
| regexp.cpp:88:20:88:21 | \\0 | RegExpConstant,RegExpEscape |
157157
| regexp.cpp:89:20:89:23 | [\\0] | RegExpCharacterClass |
158-
| regexp.cpp:89:21:89:21 | \\ | RegExpConstant,RegExpNormalChar |
159-
| regexp.cpp:89:22:89:22 | 0 | RegExpConstant,RegExpNormalChar |
158+
| regexp.cpp:89:21:89:22 | \\0 | RegExpConstant,RegExpEscape |
160159
| regexp.cpp:92:25:92:25 | a | RegExpConstant,RegExpNormalChar |
161160
| regexp.cpp:92:25:92:28 | a\\nb | RegExpSequence |
162161
| regexp.cpp:92:26:92:27 | \\n | RegExpConstant,RegExpEscape |
@@ -275,9 +274,8 @@ regExpNormalCharValue
275274
| regexp.cpp:81:19:81:24 | \\u{987 | \u0987 |
276275
| regexp.cpp:84:21:84:23 | \\cA | cA |
277276
| regexp.cpp:85:22:85:24 | \\cZ | cZ |
278-
| regexp.cpp:88:20:88:21 | \\0 | \\0 |
279-
| regexp.cpp:89:21:89:21 | \\ | \\ |
280-
| regexp.cpp:89:22:89:22 | 0 | 0 |
277+
| regexp.cpp:88:20:88:21 | \\0 | 0 |
278+
| regexp.cpp:89:21:89:22 | \\0 | 0 |
281279
| regexp.cpp:92:25:92:25 | a | a |
282280
| regexp.cpp:92:26:92:27 | \\n | \n |
283281
| regexp.cpp:92:28:92:28 | b | b |

0 commit comments

Comments
 (0)