Skip to content

Commit d6a4c4e

Browse files
committed
PRE31-C: Create UnsafeMacro class.
1 parent 49cded3 commit d6a4c4e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

c/cert/src/rules/PRE31-C/SideEffectsInArgumentsToUnsafeMacros.ql

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,35 @@ class CrementEffect extends LocalSideEffect::Range {
4646
CrementEffect() { this instanceof CrementOperation }
4747
}
4848

49+
/**
50+
* A macro that is considered potentially "unsafe" because one or more arguments are expanded
51+
* multiple times.
52+
*/
53+
class UnsafeMacro extends FunctionLikeMacro {
54+
int unsafeArgumentIndex;
55+
56+
UnsafeMacro() {
57+
exists(this.getAParameterUse(unsafeArgumentIndex)) and
58+
// Only consider arguments that are expanded multiple times, and do not consider "stringified" arguments
59+
count(int indexInBody |
60+
indexInBody = this.getAParameterUse(unsafeArgumentIndex) and
61+
not this.getBody().charAt(indexInBody) = "#"
62+
) > 1
63+
}
64+
65+
int getAnUnsafeArgumentIndex() { result = unsafeArgumentIndex }
66+
}
67+
4968
from
50-
FunctionLikeMacro flm, MacroInvocation mi, Expr e, SideEffect sideEffect, int i, string arg,
69+
UnsafeMacro flm, MacroInvocation mi, Expr e, SideEffect sideEffect, int i, string arg,
5170
string sideEffectDesc
5271
where
5372
not isExcluded(e, SideEffects4Package::sideEffectsInArgumentsToUnsafeMacrosQuery()) and
5473
sideEffect = getASideEffect(e) and
5574
flm.getAnInvocation() = mi and
5675
not exists(mi.getParentInvocation()) and
5776
mi.getAnExpandedElement() = e and
58-
// Only consider arguments that are expanded multiple times, and do not consider "stringified" arguments
59-
count(int index | index = flm.getAParameterUse(i) and not flm.getBody().charAt(index) = "#") > 1 and
77+
i = flm.getAnUnsafeArgumentIndex() and
6078
arg = mi.getExpandedArgument(i) and
6179
(
6280
sideEffect instanceof CrementEffect and

0 commit comments

Comments
 (0)