Skip to content

Commit 11581e4

Browse files
author
Robert Marsh
authored
Merge pull request #1562 from geoffw0/models
CPP: Extend StrcpyFunction and update UsingStrcpyAsBoolean.ql
2 parents c5d0aba + 29e3e2a commit 11581e4

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

cpp/ql/src/Likely Bugs/Likely Typos/UsingStrcpyAsBoolean.ql

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,9 @@
1212
*/
1313

1414
import cpp
15+
import semmle.code.cpp.models.implementations.Strcpy
1516
import semmle.code.cpp.dataflow.DataFlow
1617

17-
predicate isStringComparisonFunction(string functionName) {
18-
functionName = "strcpy" or
19-
functionName = "wcscpy" or
20-
functionName = "_mbscpy" or
21-
functionName = "strncpy" or
22-
functionName = "_strncpy_l" or
23-
functionName = "wcsncpy" or
24-
functionName = "_wcsncpy_l" or
25-
functionName = "_mbsncpy" or
26-
functionName = "_mbsncpy_l"
27-
}
28-
2918
predicate isBoolean(Expr e1) {
3019
exists(Type t1 |
3120
t1 = e1.getType() and
@@ -36,12 +25,12 @@ predicate isBoolean(Expr e1) {
3625
predicate isStringCopyCastedAsBoolean(FunctionCall func, Expr expr1, string msg) {
3726
DataFlow::localFlow(DataFlow::exprNode(func), DataFlow::exprNode(expr1)) and
3827
isBoolean(expr1.getConversion*()) and
39-
isStringComparisonFunction(func.getTarget().getName()) and
28+
func.getTarget() instanceof StrcpyFunction and
4029
msg = "Return value of " + func.getTarget().getName() + " used as a Boolean."
4130
}
4231

4332
predicate isStringCopyUsedInLogicalOperationOrCondition(FunctionCall func, Expr expr1, string msg) {
44-
isStringComparisonFunction(func.getTarget().getName()) and
33+
func.getTarget() instanceof StrcpyFunction and
4534
(
4635
(
4736
// it is being used in an equality or logical operation

cpp/ql/src/semmle/code/cpp/models/implementations/Strcpy.qll

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import semmle.code.cpp.models.interfaces.Taint
44

55

66
/**
7-
* The standard function `stract` and its wide, sized, and Microsoft variants.
7+
* The standard function `strcpy` and its wide, sized, and Microsoft variants.
88
*/
99
class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction {
1010
StrcpyFunction() {
1111
this.hasName("strcpy") or
1212
this.hasName("_mbscpy") or
1313
this.hasName("wcscpy") or
1414
this.hasName("strncpy") or
15+
this.hasName("_strncpy_l") or
1516
this.hasName("_mbsncpy") or
16-
this.hasName("wcsncpy")
17+
this.hasName("_mbsncpy_l") or
18+
this.hasName("wcsncpy") or
19+
this.hasName("_wcsncpy_l")
1720
}
1821

1922
override predicate hasArrayInput(int bufParam) {
@@ -31,13 +34,16 @@ class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction {
3134
override predicate hasArrayWithVariableSize(int bufParam, int countParam) {
3235
(
3336
this.hasName("strncpy") or
37+
this.hasName("_strncpy_l") or
3438
this.hasName("_mbsncpy") or
35-
this.hasName("wcsncpy")
39+
this.hasName("_mbsncpy_l") or
40+
this.hasName("wcsncpy") or
41+
this.hasName("_wcsncpy_l")
3642
) and
3743
bufParam = 0 and
3844
countParam = 2
3945
}
40-
46+
4147
override predicate hasArrayWithUnknownSize(int bufParam) {
4248
(
4349
this.hasName("strcpy") or
@@ -46,7 +52,6 @@ class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction {
4652
) and
4753
bufParam = 0
4854
}
49-
5055

5156
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
5257
(
@@ -70,14 +75,17 @@ class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction {
7075
output.isOutReturnValue()
7176
)
7277
}
73-
78+
7479
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
7580
(
7681
// these may do only a partial copy of the input buffer to the output
7782
// buffer
7883
this.hasName("strncpy") or
84+
this.hasName("_strncpy_l") or
7985
this.hasName("_mbsncpy") or
80-
this.hasName("wcsncpy")
86+
this.hasName("_mbsncpy_l") or
87+
this.hasName("wcsncpy") or
88+
this.hasName("_wcsncpy_l")
8189
) and (
8290
input.isInParameter(2) or
8391
input.isInParameterPointer(1)

0 commit comments

Comments
 (0)