Skip to content

Commit 8939a9b

Browse files
committed
C++: Add tests for implicit downcast involving references
1 parent eda7616 commit 8939a9b

File tree

1 file changed

+25
-5
lines changed
  • cpp/ql/test/query-tests/Likely Bugs/Conversion/ImplicitDowncastFromBitfield

1 file changed

+25
-5
lines changed

cpp/ql/test/query-tests/Likely Bugs/Conversion/ImplicitDowncastFromBitfield/test.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,42 @@ typedef struct {
22
int x : 24;
33
} my_struct;
44

5-
int getX1(my_struct m) {
5+
unsigned int getX1(my_struct m) {
66
return m.x;
77
}
88

99
short getX2(my_struct m) {
10-
return m.x;
10+
return m.x; // BAD
1111
}
1212

1313
short getX3(my_struct m) {
14-
return (short) m.x;
14+
return (short) m.x; // GOOD
1515
}
1616

1717
bool getX4(my_struct m) {
18-
return m.x;
18+
return m.x; // GOOD
1919
}
2020

2121
short getX5(my_struct m) {
22-
return (char) m.x;
22+
return (char) m.x; // GOOD
23+
}
24+
25+
const char& getx6(my_struct& m) {
26+
const char& result = m.x; // BAD [NOT DETECTED]
27+
return result;
28+
}
29+
30+
const short& getx7(my_struct& m) {
31+
const short& result = (short) m.x; // GOOD
32+
return result;
33+
}
34+
35+
const int& getx8(my_struct& m) {
36+
const int& result = m.x; // GOOD
37+
return result;
38+
}
39+
40+
const bool& getx9(my_struct& m) {
41+
const bool& result = m.x; // GOOD
42+
return result;
2343
}

0 commit comments

Comments
 (0)