Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa0f2f2

Browse files
committed
C++: Support ?: in UsingStrcpyAsBoolean.ql
I removed some unnecessary parentheses for symmetry, causing whitespace changes.
1 parent 0f88a4b commit aa0f2f2

3 files changed

Lines changed: 35 additions & 29 deletions

File tree

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,39 @@ predicate isStringCopyUsedInLogicalOperationOrCondition(FunctionCall func, Expr
4444
isStringComparisonFunction(func.getTarget().getQualifiedName()) and
4545
(
4646
(
47-
(
48-
// it is being used in an equality or logical operation
49-
exists(EqualityOperation eop |
50-
eop = expr1 and
51-
func = eop.getAnOperand()
52-
)
53-
or
54-
exists(UnaryLogicalOperation ule |
55-
expr1 = ule and
56-
func = ule.getOperand()
57-
)
58-
or
59-
exists(BinaryLogicalOperation ble |
60-
expr1 = ble and
61-
func = ble.getAnOperand()
62-
)
63-
) and
64-
msg = "Return value of " + func.getTarget().getQualifiedName() +
65-
" used in a logical operation."
66-
)
47+
// it is being used in an equality or logical operation
48+
exists(EqualityOperation eop |
49+
eop = expr1 and
50+
func = eop.getAnOperand()
51+
)
52+
or
53+
exists(UnaryLogicalOperation ule |
54+
expr1 = ule and
55+
func = ule.getOperand()
56+
)
57+
or
58+
exists(BinaryLogicalOperation ble |
59+
expr1 = ble and
60+
func = ble.getAnOperand()
61+
)
62+
) and
63+
msg = "Return value of " + func.getTarget().getQualifiedName() +
64+
" used in a logical operation."
6765
or
68-
exists(ConditionalStmt condstmt |
69-
// or the string copy function is used directly as the conditional expression
70-
func = condstmt.getControllingExpr() and
71-
expr1 = func and
72-
msg = "Return value of " + func.getTarget().getQualifiedName() +
73-
" used directly in a conditional expression."
74-
)
66+
// or the string copy function is used directly as the conditional expression
67+
(
68+
exists(ConditionalStmt condstmt |
69+
func = condstmt.getControllingExpr() and
70+
expr1 = func
71+
)
72+
or
73+
exists(ConditionalExpr ce |
74+
expr1 = ce and
75+
func = ce.getCondition()
76+
)
77+
) and
78+
msg = "Return value of " + func.getTarget().getQualifiedName() +
79+
" used directly in a conditional expression."
7580
)
7681
}
7782

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/UsingStrcpyAsBoolean/UsingStrcpyAsBoolean.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| test.c:50:9:50:15 | call to strncpy | Return value of strncpy used directly in a conditional expression. |
66
| test.c:54:9:54:37 | ! ... | Return value of strncpy used in a logical operation. |
77
| test.c:58:14:58:42 | ! ... | Return value of strncpy used in a logical operation. |
8+
| test.c:59:14:59:43 | ... ? ... : ... | Return value of strcpy used directly in a conditional expression. |
89
| test.c:60:14:60:40 | ... && ... | Return value of strcpy used in a logical operation. |
910
| test.c:62:14:62:40 | ... == ... | Return value of strcpy used in a logical operation. |
1011
| test.c:64:14:64:40 | ... != ... | Return value of strcpy used in a logical operation. |
@@ -24,7 +25,7 @@
2425
| test.cpp:127:9:127:37 | ! ... | Return value of strncpy used in a logical operation. |
2526
| test.cpp:131:14:131:20 | call to strncpy | Return value of strncpy used as Boolean. |
2627
| test.cpp:133:19:133:47 | ! ... | Return value of strncpy used in a logical operation. |
27-
| test.cpp:134:14:134:19 | call to strcpy | Return value of strcpy used as Boolean. |
28+
| test.cpp:134:14:134:43 | ... ? ... : ... | Return value of strcpy used directly in a conditional expression. |
2829
| test.cpp:135:14:135:40 | ... && ... | Return value of strcpy used in a logical operation. |
2930
| test.cpp:137:14:137:40 | ... == ... | Return value of strcpy used in a logical operation. |
3031
| test.cpp:139:14:139:40 | ... != ... | Return value of strcpy used in a logical operation. |

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/UsingStrcpyAsBoolean/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void PositiveCases()
5656
}
5757

5858
result = !strncpy(szbuf1, "test", 100); // Bug
59-
result = strcpy(szbuf1, "test") ? 1 : 0; // Bug [NOT DETECTED]
59+
result = strcpy(szbuf1, "test") ? 1 : 0; // Bug
6060
result = strcpy(szbuf1, "test") && 1; // Bug
6161

6262
result = strcpy(szbuf1, "test") == 0; // Bug

0 commit comments

Comments
 (0)