Skip to content

Commit d6e5a5c

Browse files
committed
Python: Document AdvancedFormatting.qll.
1 parent 513ead6 commit d6e5a5c

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

python/ql/src/Expressions/Formatting/AdvancedFormatting.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import python
22

3+
/** A string constant that looks like it may be used in string formatting operations. */
34
library class PossibleAdvancedFormatString extends StrConst {
45
PossibleAdvancedFormatString() { this.getText().matches("%{%}%") }
56

@@ -51,6 +52,7 @@ library class PossibleAdvancedFormatString extends StrConst {
5152
predicate isExplicitlyNumbered() { exists(this.fieldId(_, _).toInt()) }
5253
}
5354

55+
/** Holds if there is a sequence of `{` braces in `fmt` of length `len` beginning at index `index`. */
5456
predicate brace_sequence(PossibleAdvancedFormatString fmt, int index, int len) {
5557
exists(string text | text = fmt.getText() |
5658
text.charAt(index) = "{" and not text.charAt(index - 1) = "{" and len = 1
@@ -61,10 +63,12 @@ predicate brace_sequence(PossibleAdvancedFormatString fmt, int index, int len) {
6163
)
6264
}
6365

66+
/** Holds if index `index` in the format string `fmt` contains an escaped `{`. */
6467
predicate escaped_brace(PossibleAdvancedFormatString fmt, int index) {
6568
exists(int len | brace_sequence(fmt, index, len) | len % 2 = 0)
6669
}
6770

71+
/** Holds if index `index` in the format string `fmt` contains a left curly brace that acts as an escape. */
6872
predicate escaping_brace(PossibleAdvancedFormatString fmt, int index) {
6973
escaped_brace(fmt, index + 1)
7074
}
@@ -105,15 +109,18 @@ private predicate advanced_format_call(Call format_expr, PossibleAdvancedFormatS
105109
)
106110
}
107111

112+
/** A string constant that has the `format` method applied to it. */
108113
class AdvancedFormatString extends PossibleAdvancedFormatString {
109114
AdvancedFormatString() { advanced_format_call(_, this, _) }
110115
}
111116

117+
/** A string formatting operation using the `format` method. */
112118
class AdvancedFormattingCall extends Call {
113119
AdvancedFormattingCall() { advanced_format_call(this, _, _) }
114120

115121
/** Count of the arguments actually provided */
116122
int providedArgCount() { advanced_format_call(this, _, result) }
117123

124+
/** Gets a formatting string for this call. */
118125
AdvancedFormatString getAFormat() { advanced_format_call(this, result, _) }
119126
}

0 commit comments

Comments
 (0)