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

Skip to content

Commit eef8719

Browse files
committed
C++: Fix AV Rule 85
We have to be careful to avoid giving alerts to functions that might be correctly defined, but we can't see the definition as it wasn't instantiated.
1 parent ef15523 commit eef8719

3 files changed

Lines changed: 46 additions & 36 deletions

File tree

cpp/ql/src/jsf/4.10 Classes/AV Rule 85.ql

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,34 @@ predicate implementedAsNegationOf(Operator op1, Operator op2) {
2828
c.getTarget() = op2)
2929
}
3030

31+
predicate classIsCheckableFor(Class c, string op) {
32+
oppositeOperators(op, _) and
33+
if exists(Class templ | c.isConstructedFrom(templ))
34+
then // If we are an instantiation, then we can't check `op` if the
35+
// template has it but we don't (because that member wasn't
36+
// instantiated)
37+
exists(Class templ | c.isConstructedFrom(templ) and
38+
(templ.getAMember().hasName(op) implies
39+
exists(Function f | f = c.getAMember() and
40+
f.hasName(op) and
41+
f.isDefined())))
42+
else any()
43+
}
44+
3145
from Class c, string op, string opp, Operator rator
3246
where c.fromSource() and
47+
not c instanceof TemplateClass and
3348
oppositeOperators(op, opp) and
49+
classIsCheckableFor(c, op) and
50+
classIsCheckableFor(c, opp) and
3451
rator = c.getAMember() and
3552
rator.hasName(op) and
36-
not exists(Operator oprator | oprator = c.getAMember() and
37-
oprator.hasName(opp) and
38-
( implementedAsNegationOf(rator, oprator)
39-
or implementedAsNegationOf(oprator, rator)))
53+
forex(Operator aRator |
54+
aRator = c.getAMember() and aRator.hasName(op) |
55+
not exists(Operator oprator |
56+
oprator = c.getAMember() and
57+
oprator.hasName(opp) and
58+
( implementedAsNegationOf(aRator, oprator)
59+
or implementedAsNegationOf(oprator, aRator))))
4060
select c, "When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator " + op +
4161
" is declared on line " + rator.getLocation().getStartLine().toString() + ", but it is not defined in terms of its opposite operator " + opp + "."

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 85/AV Rule 85.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,25 @@ void f9(void) {
124124
b = myClass9 >= myClass9;
125125
}
126126

127+
template <typename T>
128+
class MyClass10 {
129+
public:
130+
int i;
131+
template <typename U>
132+
bool operator< (const MyClass10<U> &rhs){ return i < rhs.i; }
133+
template <typename U>
134+
bool operator>= (const MyClass10<U> &rhs){ return !(*this < rhs); }
135+
// GOOD
136+
template <typename U>
137+
bool operator> (const MyClass10<U> &rhs){ return i < rhs.i; }
138+
template <typename U>
139+
bool operator<= (const MyClass10<U> &rhs){ return i >= rhs.i; }
140+
// BAD: neither operator defined in terms of the other
141+
};
142+
void f10(void) {
143+
bool b;
144+
MyClass10<int> myClass10;
145+
b = myClass10 < myClass10;
146+
b = myClass10 > myClass10;
147+
}
148+

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 85/AV Rule 85.expected

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,7 @@
33
| AV Rule 85.cpp:9:7:9:14 | MyClass2 | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 13, but it is not defined in terms of its opposite operator operator<. |
44
| AV Rule 85.cpp:25:7:25:14 | MyClass4 | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 32, but it is not defined in terms of its opposite operator operator>. |
55
| AV Rule 85.cpp:25:7:25:14 | MyClass4 | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 31, but it is not defined in terms of its opposite operator operator<=. |
6-
| AV Rule 85.cpp:37:7:37:14 | MyClass5<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 40, but it is not defined in terms of its opposite operator operator>=. |
7-
| AV Rule 85.cpp:37:7:37:14 | MyClass5<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 44, but it is not defined in terms of its opposite operator operator>. |
8-
| AV Rule 85.cpp:37:7:37:14 | MyClass5<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 43, but it is not defined in terms of its opposite operator operator<=. |
9-
| AV Rule 85.cpp:37:7:37:14 | MyClass5<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 41, but it is not defined in terms of its opposite operator operator<. |
10-
| AV Rule 85.cpp:49:7:49:14 | MyClass6<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 52, but it is not defined in terms of its opposite operator operator>=. |
11-
| AV Rule 85.cpp:49:7:49:14 | MyClass6<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 56, but it is not defined in terms of its opposite operator operator>. |
12-
| AV Rule 85.cpp:49:7:49:14 | MyClass6<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 55, but it is not defined in terms of its opposite operator operator<=. |
13-
| AV Rule 85.cpp:49:7:49:14 | MyClass6<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 53, but it is not defined in terms of its opposite operator operator<. |
14-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 66, but it is not defined in terms of its opposite operator operator>=. |
15-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 73, but it is not defined in terms of its opposite operator operator>. |
16-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 71, but it is not defined in terms of its opposite operator operator<=. |
17-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 68, but it is not defined in terms of its opposite operator operator<. |
18-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 66, but it is not defined in terms of its opposite operator operator>=. |
19-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 73, but it is not defined in terms of its opposite operator operator>. |
20-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 71, but it is not defined in terms of its opposite operator operator<=. |
21-
| AV Rule 85.cpp:62:7:62:14 | MyClass7<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 68, but it is not defined in terms of its opposite operator operator<. |
22-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 83, but it is not defined in terms of its opposite operator operator>=. |
23-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 90, but it is not defined in terms of its opposite operator operator>. |
24-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 88, but it is not defined in terms of its opposite operator operator<=. |
25-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 85, but it is not defined in terms of its opposite operator operator<. |
26-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 83, but it is not defined in terms of its opposite operator operator>=. |
276
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 90, but it is not defined in terms of its opposite operator operator>. |
287
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 88, but it is not defined in terms of its opposite operator operator<=. |
29-
| AV Rule 85.cpp:79:7:79:14 | MyClass8<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 85, but it is not defined in terms of its opposite operator operator<. |
30-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 107, but it is not defined in terms of its opposite operator operator>=. |
31-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 114, but it is not defined in terms of its opposite operator operator>. |
32-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 112, but it is not defined in terms of its opposite operator operator<=. |
33-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<T> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 109, but it is not defined in terms of its opposite operator operator<. |
34-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 107, but it is not defined in terms of its opposite operator operator>=. |
35-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 114, but it is not defined in terms of its opposite operator operator>. |
36-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 112, but it is not defined in terms of its opposite operator operator<=. |
37-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<double> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 109, but it is not defined in terms of its opposite operator operator<. |
38-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator< is declared on line 107, but it is not defined in terms of its opposite operator operator>=. |
398
| AV Rule 85.cpp:103:7:103:14 | MyClass9<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator<= is declared on line 114, but it is not defined in terms of its opposite operator operator>. |
409
| AV Rule 85.cpp:103:7:103:14 | MyClass9<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator> is declared on line 112, but it is not defined in terms of its opposite operator operator<=. |
41-
| AV Rule 85.cpp:103:7:103:14 | MyClass9<int> | When two operators are opposites, both should be defined and one should be defined in terms of the other. Operator operator>= is declared on line 109, but it is not defined in terms of its opposite operator operator<. |

0 commit comments

Comments
 (0)