Skip to content

Commit 0724c22

Browse files
authored
Merge pull request #21702 from jketema/conv-string
C++: Use new `getConvSpecString` instead of `getConvSpecOffset` and `substring`
2 parents f6fb613 + 26715fc commit 0724c22

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Printf.qll

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ class FormatLiteral extends Literal instanceof StringLiteral {
459459
*/
460460
int getConvSpecOffset(int n) { result = this.getFormat().indexOf("%", n, 0) }
461461

462+
/**
463+
* Gets the nth conversion specifier string.
464+
*/
465+
private string getConvSpecString(int n) {
466+
n >= 0 and result = "%" + this.getFormat().splitAt("%", n + 1)
467+
}
468+
462469
/*
463470
* Each of these predicates gets a regular expressions to match each individual
464471
* parts of a conversion specifier.
@@ -524,22 +531,20 @@ class FormatLiteral extends Literal instanceof StringLiteral {
524531
int n, string spec, string params, string flags, string width, string prec, string len,
525532
string conv
526533
) {
527-
exists(int offset, string fmt, string rst, string regexp |
528-
offset = this.getConvSpecOffset(n) and
529-
fmt = this.getFormat() and
530-
rst = fmt.substring(offset, fmt.length()) and
534+
exists(string convSpec, string regexp |
535+
convSpec = this.getConvSpecString(n) and
531536
regexp = this.getConvSpecRegexp() and
532537
(
533-
spec = rst.regexpCapture(regexp, 1) and
534-
params = rst.regexpCapture(regexp, 2) and
535-
flags = rst.regexpCapture(regexp, 3) and
536-
width = rst.regexpCapture(regexp, 4) and
537-
prec = rst.regexpCapture(regexp, 5) and
538-
len = rst.regexpCapture(regexp, 6) and
539-
conv = rst.regexpCapture(regexp, 7)
538+
spec = convSpec.regexpCapture(regexp, 1) and
539+
params = convSpec.regexpCapture(regexp, 2) and
540+
flags = convSpec.regexpCapture(regexp, 3) and
541+
width = convSpec.regexpCapture(regexp, 4) and
542+
prec = convSpec.regexpCapture(regexp, 5) and
543+
len = convSpec.regexpCapture(regexp, 6) and
544+
conv = convSpec.regexpCapture(regexp, 7)
540545
or
541-
spec = rst.regexpCapture(regexp, 1) and
542-
not exists(rst.regexpCapture(regexp, 2)) and
546+
spec = convSpec.regexpCapture(regexp, 1) and
547+
not exists(convSpec.regexpCapture(regexp, 2)) and
543548
params = "" and
544549
flags = "" and
545550
width = "" and
@@ -554,12 +559,10 @@ class FormatLiteral extends Literal instanceof StringLiteral {
554559
* Gets the nth conversion specifier (including the initial `%`).
555560
*/
556561
string getConvSpec(int n) {
557-
exists(int offset, string fmt, string rst, string regexp |
558-
offset = this.getConvSpecOffset(n) and
559-
fmt = this.getFormat() and
560-
rst = fmt.substring(offset, fmt.length()) and
562+
exists(string convSpec, string regexp |
563+
convSpec = this.getConvSpecString(n) and
561564
regexp = this.getConvSpecRegexp() and
562-
result = rst.regexpCapture(regexp, 1)
565+
result = convSpec.regexpCapture(regexp, 1)
563566
)
564567
}
565568

cpp/ql/lib/semmle/code/cpp/commons/Scanf.qll

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ class ScanfFormatLiteral extends Expr {
194194
)
195195
}
196196

197+
/**
198+
* Gets the nth conversion specifier string.
199+
*/
200+
private string getConvSpecString(int n) {
201+
n >= 0 and result = "%" + this.getFormat().splitAt("%", n + 1)
202+
}
203+
197204
/**
198205
* Gets the regular expression to match each individual part of a conversion specifier.
199206
*/
@@ -227,16 +234,14 @@ class ScanfFormatLiteral extends Expr {
227234
* specifier.
228235
*/
229236
predicate parseConvSpec(int n, string spec, string width, string len, string conv) {
230-
exists(int offset, string fmt, string rst, string regexp |
231-
offset = this.getConvSpecOffset(n) and
232-
fmt = this.getFormat() and
233-
rst = fmt.substring(offset, fmt.length()) and
237+
exists(string convSpec, string regexp |
238+
convSpec = this.getConvSpecString(n) and
234239
regexp = this.getConvSpecRegexp() and
235240
(
236-
spec = rst.regexpCapture(regexp, 1) and
237-
width = rst.regexpCapture(regexp, 2) and
238-
len = rst.regexpCapture(regexp, 3) and
239-
conv = rst.regexpCapture(regexp, 4)
241+
spec = convSpec.regexpCapture(regexp, 1) and
242+
width = convSpec.regexpCapture(regexp, 2) and
243+
len = convSpec.regexpCapture(regexp, 3) and
244+
conv = convSpec.regexpCapture(regexp, 4)
240245
)
241246
)
242247
}

0 commit comments

Comments
 (0)