Skip to content

Commit 54c766c

Browse files
committed
CPP: Add more test cases for LargeParameter.ql.
1 parent 88a0e60 commit 54c766c

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
| test.cpp:16:13:16:14 | _t | This parameter of type $@ is 4096 bytes - consider passing a const pointer/reference instead. | test.cpp:6:8:6:20 | myLargeStruct | myLargeStruct |
22
| test.cpp:24:44:24:48 | mtc_t | This parameter of type $@ is 4096 bytes - consider passing a const pointer/reference instead. | test.cpp:11:7:11:21 | myTemplateClass<myLargeStruct> | myTemplateClass<myLargeStruct> |
33
| test.cpp:28:49:28:49 | b | This parameter of type $@ is 4096 bytes - consider passing a const pointer/reference instead. | test.cpp:6:8:6:20 | myLargeStruct | myLargeStruct |
4+
| test.cpp:78:16:78:16 | a | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
5+
| test.cpp:79:16:79:16 | b | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
6+
| test.cpp:80:16:80:16 | c | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
7+
| test.cpp:81:16:81:16 | d | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
8+
| test.cpp:82:16:82:16 | e | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
9+
| test.cpp:83:16:83:16 | f | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
10+
| test.cpp:84:16:84:16 | g | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
11+
| test.cpp:104:16:104:16 | a | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
12+
| test.cpp:105:16:105:16 | b | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
13+
| test.cpp:106:16:106:16 | c | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
14+
| test.cpp:107:16:107:16 | d | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
15+
| test.cpp:108:16:108:16 | e | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
16+
| test.cpp:109:16:109:16 | f | This parameter of type $@ is 4100 bytes - consider passing a const pointer/reference instead. | test.cpp:58:8:58:19 | MyLargeClass | MyLargeClass |
17+
| test.cpp:144:47:144:49 | lhs | This parameter of type $@ is 1028 bytes - consider passing a const pointer/reference instead. | test.cpp:130:7:130:23 | MyArithmeticClass | MyArithmeticClass |

cpp/ql/test/query-tests/Critical/LargeParameter/test.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,96 @@ struct CustomAssignmentOp {
5252
CustomAssignmentOp &operator=(CustomAssignmentOp rhs);
5353
char data[4096];
5454
};
55+
56+
// ---
57+
58+
struct MyLargeClass {
59+
public:
60+
MyLargeClass();
61+
62+
void myMethod();
63+
void myConstMethod() const;
64+
65+
int value;
66+
char data[4096];
67+
};
68+
69+
void mlc_modify(MyLargeClass &c) {
70+
c.value++;
71+
}
72+
73+
int mlc_get(const MyLargeClass &c) {
74+
return c.value;
75+
}
76+
77+
void myFunction4(
78+
MyLargeClass a, // GOOD: large, but the copy is written to so can't be trivially replaced with a reference [FALSE POSITIVE]
79+
MyLargeClass b, // GOOD [FALSE POSITIVE]
80+
MyLargeClass c, // GOOD [FALSE POSITIVE]
81+
MyLargeClass d, // GOOD [FALSE POSITIVE]
82+
MyLargeClass e, // GOOD [FALSE POSITIVE]
83+
MyLargeClass f, // GOOD [FALSE POSITIVE]
84+
MyLargeClass g // GOOD [FALSE POSITIVE]
85+
)
86+
{
87+
MyLargeClass *mlc_ptr;
88+
int *i_ptr;
89+
90+
a.value++;
91+
b.value = 1;
92+
c.data[0] += 1;
93+
d.myMethod();
94+
mlc_modify(e);
95+
96+
mlc_ptr = &f;
97+
mlc_modify(*mlc_ptr);
98+
99+
i_ptr = &g.value;
100+
*(i_ptr)++;
101+
}
102+
103+
void myFunction5(
104+
MyLargeClass a, // BAD
105+
MyLargeClass b, // BAD
106+
MyLargeClass c, // BAD
107+
MyLargeClass d, // BAD
108+
MyLargeClass e, // BAD
109+
MyLargeClass f // BAD
110+
)
111+
{
112+
const MyLargeClass *mlc_ptr;
113+
const int *i_ptr;
114+
int i;
115+
116+
i = a.value;
117+
i += b.data[0];
118+
c.myConstMethod();
119+
i += mlc_get(d);
120+
121+
mlc_ptr = &e;
122+
mlc_get(*mlc_ptr);
123+
124+
i_ptr = &f.value;
125+
i += *i_ptr;
126+
}
127+
128+
// ---
129+
130+
class MyArithmeticClass {
131+
public:
132+
MyArithmeticClass(int _value) : value(_value) {};
133+
134+
MyArithmeticClass &operator+=(const MyArithmeticClass &other) {
135+
this->value += other.value;
136+
return *this;
137+
}
138+
139+
private:
140+
int value;
141+
char data[1024];
142+
};
143+
144+
MyArithmeticClass operator+(MyArithmeticClass lhs, const MyArithmeticClass &rhs) { // GOOD [FALSE POSITIVE]
145+
lhs += rhs; // lhs is copied by design
146+
return lhs;
147+
}

0 commit comments

Comments
 (0)