Skip to content

Commit e6cfe17

Browse files
committed
C++: Handle \xhh in RegExpEscape
1 parent 40feb22 commit e6cfe17

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

cpp/ql/lib/semmle/code/cpp/regex/RegexTreeView.qll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ private module Impl implements RegexTreeViewSig {
574574
*/
575575
override string getValue() {
576576
not this.isUnicode() and
577+
not this.isHex() and
577578
not this.isControl() and
578579
this.isIdentityEscape() and
579580
result = this.getUnescaped()
@@ -593,6 +594,9 @@ private module Impl implements RegexTreeViewSig {
593594
this.isUnicode() and
594595
result = this.getUnicode()
595596
or
597+
this.isHex() and
598+
result = this.getHex()
599+
or
596600
this.isControl() and
597601
result = this.getControl()
598602
}
@@ -601,6 +605,7 @@ private module Impl implements RegexTreeViewSig {
601605
predicate isIdentityEscape() {
602606
not this.getUnescaped() in ["n", "r", "t", "f", "v", "0"] and
603607
not this.isUnicode() and
608+
not this.isHex() and
604609
not this.isControl()
605610
}
606611

@@ -628,6 +633,20 @@ private module Impl implements RegexTreeViewSig {
628633
result = parseHexInt(this.getText().suffix(2)).toUnicode()
629634
}
630635

636+
/**
637+
* Holds if this is a hex escape.
638+
*/
639+
private predicate isHex() { this.getText().prefix(2) = "\\x" }
640+
641+
/**
642+
* Gets the unicode char for this escape.
643+
* E.g. for `\x61` this returns "a".
644+
*/
645+
private string getHex() {
646+
this.isHex() and
647+
result = parseHexInt(this.getText().suffix(2)).toUnicode()
648+
}
649+
631650
/**
632651
* Holds if this is a `\cX` escape.
633652
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ regExpNormalCharValue
315315
| regexp.cpp:84:37:84:37 | a | a |
316316
| regexp.cpp:84:39:84:39 | f | f |
317317
| regexp.cpp:90:19:90:24 | \\u0061 | a |
318-
| regexp.cpp:93:19:93:22 | \\x61 | x61 |
318+
| regexp.cpp:93:19:93:22 | \\x61 | a |
319319
| regexp.cpp:96:21:96:23 | \\cA | \u0000 |
320320
| regexp.cpp:97:22:97:24 | \\cz | \u0019 |
321321
| regexp.cpp:100:20:100:21 | \\0 | \u0000 |

0 commit comments

Comments
 (0)