Skip to content

Commit d2f8029

Browse files
authored
Merge pull request #1492 from geoffw0/exprnoeffectweak
CPP: Fix for 'Expression has no effect' on calls to weak functions
2 parents de65dc5 + 6800abd commit d2f8029

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

change-notes/1.22/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
| **Query** | **Expected impact** | **Change** |
1313
|----------------------------|------------------------|------------------------------------------------------------------|
14+
| Expression has no effect (`cpp/useless-expression`) | Fewer false positive results | Calls to functions with the `weak` attribute are no longer considered to be side effect free, because they could be overridden with a different implementation at link time. |
1415
| Suspicious pointer scaling (`cpp/suspicious-pointer-scaling`) | Lower precision | The precision of this query has been reduced to "medium". This coding pattern is used intentionally and safely in a number of real-world projects. Results are no longer displayed on LGTM unless you choose to display them. |
1516

1617
## Changes to QL libraries

cpp/ql/src/semmle/code/cpp/exprs/Call.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,14 @@ class FunctionCall extends Call, @funbindexpr {
275275
override predicate mayBeImpure() {
276276
this.getChild(_).mayBeImpure() or
277277
this.getTarget().mayHaveSideEffects() or
278-
isVirtual()
278+
isVirtual() or
279+
getTarget().getAnAttribute().getName() = "weak"
279280
}
280281
override predicate mayBeGloballyImpure() {
281282
this.getChild(_).mayBeGloballyImpure() or
282283
this.getTarget().mayHaveSideEffects() or
283-
isVirtual()
284+
isVirtual() or
285+
getTarget().getAnAttribute().getName() = "weak"
284286
}
285287
}
286288

cpp/ql/test/query-tests/Likely Bugs/Likely Typos/ExprHasNoEffect/ExprHasNoEffect.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
| volatile.c:12:5:12:9 | access to array | This expression has no effect. | volatile.c:12:5:12:9 | access to array | |
3434
| volatile.c:16:5:16:7 | * ... | This expression has no effect. | volatile.c:16:5:16:7 | * ... | |
3535
| volatile.c:20:5:20:13 | * ... | This expression has no effect. | volatile.c:20:5:20:13 | * ... | |
36+
| weak.c:18:2:18:18 | call to myNothingFunction | This expression has no effect (because $@ has no external side effects). | weak.c:2:5:2:21 | myNothingFunction | myNothingFunction |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
int myNothingFunction()
3+
{
4+
// does nothing
5+
6+
return 0;
7+
}
8+
9+
int __attribute__((__weak__)) myWeakNothingFunction()
10+
{
11+
// does nothing, but we could be overridden at the linker stage with a non-weak definition
12+
// from elsewhere in the program.
13+
14+
return 0;
15+
}
16+
17+
void testWeak() {
18+
myNothingFunction(); // BAD
19+
myWeakNothingFunction(); // GOOD
20+
}

0 commit comments

Comments
 (0)