Skip to content

Commit 909b36a

Browse files
committed
C++: Fix implicit-this FP, uncovered non-funptr FP
1 parent 19a9c5d commit 909b36a

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import semmle.code.cpp.commons.Exclusions
1313

1414
/** Gets the sub-expression of 'e' with the earliest-starting Location */
1515
Expr normalizeExpr(Expr e) {
16-
if exists(e.(Call).getQualifier())
16+
if forex(Expr q | q = e.(Call).getQualifier() | not q instanceof ThisExpr)
1717
then result = normalizeExpr(e.(Call).getQualifier())
1818
else
19-
if exists(e.(FieldAccess).getQualifier())
19+
if forex(Expr q | q = e.(FieldAccess).getQualifier() | not q instanceof ThisExpr)
2020
then result = normalizeExpr(e.(FieldAccess).getQualifier())
2121
else
2222
if e.hasExplicitConversion()

cpp/ql/test/query-tests/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation/test.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,24 @@ struct X {
1515

1616
#define BAZ //printf
1717

18-
int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
18+
struct Foo {
19+
int i;
20+
int j;
21+
virtual int foo(int) = 0;
22+
virtual int bar(int, int) = 0;
23+
int test(int (*baz)(int));
24+
25+
struct Tata {
26+
struct Titi {
27+
void tutu() {}
28+
long toto() { return 42; }
29+
} titi;
30+
} *tata;
31+
32+
Tata::Titi **titi_ptr_ptr;
33+
};
34+
35+
int Foo::test(int (*baz)(int))
1936
{
2037
// Comma in simple if statement (prototypical example):
2138

@@ -123,9 +140,13 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
123140

124141
// LHS ends on same line RHS begins on:
125142

126-
int k = (foo(
143+
int k1 = (foo(
144+
i++
145+
), j++); // GOOD? [FALSE POSITIVE]
146+
147+
int k2 = (baz(
127148
i++
128-
), j++); // GOOD?
149+
), j++); // GOOD when it's a function-pointer call!?
129150

130151
// Weird cases:
131152

@@ -135,17 +156,13 @@ int test(int i, int j, int (*foo)(int), int (*bar)(int, int))
135156
? 1
136157
: 2;
137158

138-
struct {
139-
struct {
140-
void tutu() {}
141-
long toto() { return 42; }
142-
} titi;
143-
} *tata;
144-
145159
int quux =
146160
(tata->titi.tutu(),
147161
foo(tata->titi.toto())); // GOOD
148162

163+
(*titi_ptr_ptr)->tutu(), // GOOD
164+
(&i)[0] += (int)(*titi_ptr_ptr)->toto();
165+
149166
return quux;
150167
}
151168

0 commit comments

Comments
 (0)