Skip to content

Commit 39be9c3

Browse files
committed
CPP: Use StrcpyFunction model in UsingStrcpyAsBoolean.ql.
1 parent d2f8029 commit 39be9c3

File tree

2 files changed

+3
-17
lines changed

2 files changed

+3
-17
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/test/query-tests/Likely Bugs/Likely Typos/UsingStrcpyAsBoolean/UsingStrcpyAsBoolean.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
| test.cpp:103:9:103:15 | call to strncpy | Return value of strncpy used directly in a conditional expression. |
2020
| test.cpp:107:9:107:15 | call to wcsncpy | Return value of wcsncpy used directly in a conditional expression. |
2121
| test.cpp:111:9:111:16 | call to _mbsncpy | Return value of _mbsncpy used directly in a conditional expression. |
22-
| test.cpp:115:9:115:18 | call to _strncpy_l | Return value of _strncpy_l used directly in a conditional expression. |
23-
| test.cpp:119:9:119:18 | call to _wcsncpy_l | Return value of _wcsncpy_l used directly in a conditional expression. |
24-
| test.cpp:123:9:123:18 | call to _mbsncpy_l | Return value of _mbsncpy_l used directly in a conditional expression. |
2522
| test.cpp:127:9:127:37 | ! ... | Return value of strncpy used in a logical operation. |
2623
| test.cpp:131:14:131:20 | call to strncpy | Return value of strncpy used as a Boolean. |
2724
| test.cpp:133:19:133:47 | ! ... | Return value of strncpy used in a logical operation. |

0 commit comments

Comments
 (0)