Skip to content

Commit 89bea60

Browse files
committed
C++: Fix false positive.
1 parent 720ac02 commit 89bea60

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ import cpp
1515
import semmle.code.cpp.models.implementations.Strcpy
1616
import semmle.code.cpp.dataflow.DataFlow
1717

18+
/**
19+
* A string copy function that returns a string, rather than an error code (for
20+
* example, `strcpy` returns a string, whereas `strcpy_s` returns an error
21+
* code).
22+
*/
23+
class InterestingStrcpyFunction extends StrcpyFunction {
24+
InterestingStrcpyFunction()
25+
{
26+
getType().getUnspecifiedType() instanceof PointerType
27+
}
28+
}
29+
1830
predicate isBoolean(Expr e1) {
1931
exists(Type t1 |
2032
t1 = e1.getType() and
@@ -25,12 +37,12 @@ predicate isBoolean(Expr e1) {
2537
predicate isStringCopyCastedAsBoolean(FunctionCall func, Expr expr1, string msg) {
2638
DataFlow::localExprFlow(func, expr1) and
2739
isBoolean(expr1.getConversion*()) and
28-
func.getTarget() instanceof StrcpyFunction and
40+
func.getTarget() instanceof InterestingStrcpyFunction and
2941
msg = "Return value of " + func.getTarget().getName() + " used as a Boolean."
3042
}
3143

3244
predicate isStringCopyUsedInLogicalOperationOrCondition(FunctionCall func, Expr expr1, string msg) {
33-
func.getTarget() instanceof StrcpyFunction and
45+
func.getTarget() instanceof InterestingStrcpyFunction and
3446
(
3547
(
3648
// 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@
2929
| test.cpp:135:14:135:40 | ... && ... | Return value of strcpy used in a logical operation. |
3030
| test.cpp:137:14:137:40 | ... == ... | Return value of strcpy used in a logical operation. |
3131
| test.cpp:139:14:139:40 | ... != ... | Return value of strcpy used in a logical operation. |
32-
| test.cpp:159:9:159:16 | call to strcpy_s | Return value of strcpy_s used directly in a conditional expression. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ void NegativeCases()
156156
{
157157
}
158158

159-
if (strcpy_s(szbuf1, 100, "test")) // [FALSE POSITIVE]
159+
if (strcpy_s(szbuf1, 100, "test"))
160160
{
161161
}
162162

0 commit comments

Comments
 (0)