Skip to content

Commit a201fe6

Browse files
authored
Merge pull request #22 from rdmarsh2/rdmarsh/cpp/use-in-own-initializer-macro
C++: handle more macros in UseInOwnInitializer
2 parents 4d97570 + 6546b37 commit a201fe6

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

cpp/ql/src/Likely Bugs/UseInOwnInitializer.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ where init.getDeclaration() = v
2727
va = mi.getExpr()
2828
)
2929
)
30+
and not (
31+
va.getEnclosingStmt().isInMacroExpansion()
32+
)
3033
select va, v.getName() + " is used in its own initializer."
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
typedef long size_t;
22

33
void test1() {
4-
int x = x;
4+
int x = x; // BAD
55
}
66

77
void test2() {
8-
int x = x = 2;
8+
int x = x = 2; // BAD
99
}
1010

1111
void test3() {
12-
int x = 2;
12+
int x = 2; // GOOD
1313
}
1414

1515
void test4() {
16-
void* x = (void *)&x;
16+
void* x = (void *)&x; // GOOD
1717
}
1818

1919
void test5() {
20-
size_t x = sizeof(x);
20+
size_t x = sizeof(x); // GOOD
2121
}
2222

2323
typedef void *voidptr;
@@ -28,7 +28,7 @@ voidptr address_of(voidptr &var) {
2828
}
2929

3030
void test6() {
31-
voidptr x = address_of(x); // initialize to it's own address
31+
voidptr x = address_of(x); // GOOD (implicit conversion to reference)
3232
}
3333

3434

@@ -38,25 +38,31 @@ struct MyString {
3838
};
3939

4040
void test7() {
41-
MyString ms = {ms.array, {0}}; // initialize to ""
41+
MyString ms = {ms.array, {0}}; // GOOD (implicit conversion to pointer)
4242
}
4343

4444
#define uninitialized(x) x
4545

4646
void test8() {
47-
int x = uninitialized(x);
47+
int x = uninitialized(x); // GOOD (rval is a macro)
4848
}
4949

5050
#define uninitialized2(x) x = x
5151

5252
void test9() {
53-
int uninitialized2(x);
53+
int uninitialized2(x); // GOOD (initializer is a macro)
5454
}
5555

5656
void test10() {
57-
int x = x + 1;
57+
int x = x + 1; // BAD: x is evaluated on the right hand side
5858
}
5959

6060
void test11() {
61-
int x = uninitialized(x) + 1;
61+
int x = uninitialized(x) + 1; // BAD: x is evaluated on the right hand side
62+
}
63+
64+
#define self_initialize(t, x) t x = x
65+
66+
void test12() {
67+
self_initialize(int, x); // GOOD (statement is from a macro)
6268
}

0 commit comments

Comments
 (0)