Skip to content

Commit 7affbe4

Browse files
authored
Merge pull request #341 from geoffw0/av_114
CPP: Improve AV Rule 114.ql's understanding of return types.
2 parents 640de0c + dbae5c2 commit 7affbe4

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

change-notes/1.19/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
|----------------------------|------------------------|------------------------------------------------------------------|
1818
| Resource not released in destructor | Fewer false positive results | Placement new is now excluded from the query. Also fixed an issue where false positives could occur if the destructor body was not in the snapshot. |
1919
| Missing return statement (`cpp/missing-return`) | Visible by default | The precision of this query has been increased from 'medium' to 'high', which makes it visible by default in LGTM. It was 'medium' in release 1.17 and 1.18 because it had false positives due to an extractor bug that was fixed in 1.18. |
20+
| Missing return statement | Fewer false positive results | The query is now produces correct results when a function returns a template-dependent type. |
2021
| Call to memory access function may overflow buffer | More correct results | Array indexing with a negative index is now detected by this query. |
2122
| Suspicious add with sizeof | Fewer false positive results | Arithmetic with void pointers (where allowed) is now excluded from this query. |
2223
| Wrong type of arguments to formatting function | Fewer false positive results | False positive results involving typedefs have been removed. Expected argument types are determined more accurately, especially for wide string and pointer types. Custom (non-standard) formatting functions are also identified more accurately. |

cpp/ql/src/jsf/4.13 Functions/AV Rule 114.ql

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import cpp
1919

2020
predicate functionsMissingReturnStmt(Function f, ControlFlowNode blame) {
2121
f.fromSource() and
22-
not f.getType().getUnderlyingType().getUnspecifiedType() instanceof VoidType and
22+
exists(Type returnType |
23+
returnType = f.getType().getUnderlyingType().getUnspecifiedType() and
24+
not returnType instanceof VoidType and
25+
not returnType instanceof TemplateParameter
26+
) and
2327
exists(ReturnStmt s | f.getAPredecessor() = s | blame = s.getAPredecessor())}
2428

2529
/* If a function has a value-carrying return statement, but the extractor hit a snag
@@ -32,13 +36,11 @@ predicate functionImperfectlyExtracted(Function f) {
3236
exists(ErrorExpr ee | ee.getEnclosingFunction() = f)
3337
}
3438

35-
from Stmt stmt, string msg
39+
from Stmt stmt, string msg, Function f, ControlFlowNode blame
3640
where
37-
exists(Function f, ControlFlowNode blame |
38-
functionsMissingReturnStmt(f, blame) and
39-
reachable(blame) and
40-
not functionImperfectlyExtracted(f) and
41-
(blame = stmt or blame.(Expr).getEnclosingStmt() = stmt) and
42-
msg = "Function " + f.getName() + " should return a value of type " + f.getType().getName() + " but does not return a value here"
43-
)
41+
functionsMissingReturnStmt(f, blame) and
42+
reachable(blame) and
43+
not functionImperfectlyExtracted(f) and
44+
(blame = stmt or blame.(Expr).getEnclosingStmt() = stmt) and
45+
msg = "Function " + f.getName() + " should return a value of type " + f.getType().getName() + " but does not return a value here"
4446
select stmt, msg

cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
| test.c:39:9:39:14 | ExprStmt | Function f6 should return a value of type int but does not return a value here |
44
| test.cpp:16:1:18:1 | { ... } | Function g2 should return a value of type MyValue but does not return a value here |
55
| test.cpp:48:2:48:26 | if (...) ... | Function g7 should return a value of type MyValue but does not return a value here |
6+
| test.cpp:74:1:76:1 | { ... } | Function g10 should return a value of type second but does not return a value here |
7+
| test.cpp:86:1:88:1 | { ... } | Function g12 should return a value of type second but does not return a value here |

cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,45 @@ MyValue g7(bool c)
5050
DONOTHING
5151
// BAD [the alert here is unfortunately placed]
5252
}
53+
54+
typedef void MYVOID;
55+
MYVOID g8()
56+
{
57+
// GOOD
58+
}
59+
60+
template<class T, class U>
61+
class TypePair
62+
{
63+
public:
64+
typedef T first;
65+
typedef U second;
66+
};
67+
68+
TypePair<void, int>::first g9()
69+
{
70+
// GOOD (the return type amounts to void)
71+
}
72+
73+
TypePair<void, int>::second g10()
74+
{
75+
// BAD (the return type amounts to int)
76+
}
77+
78+
template<class T>
79+
typename TypePair<void, T>::first g11()
80+
{
81+
// GOOD (the return type amounts to void)
82+
}
83+
84+
template<class T>
85+
typename TypePair<void, T>::second g12()
86+
{
87+
// BAD (the return type amounts to T / int)
88+
}
89+
90+
void instantiate()
91+
{
92+
g11<int>();
93+
g12<int>();
94+
}

0 commit comments

Comments
 (0)