Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64ed930

Browse files
author
Robert Marsh
committed
C++: new query for futile arguments to C functions
1 parent 6af8948 commit 64ed930

7 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
void no_argument();
2+
3+
void one_argument(int x);
4+
5+
void calls() {
6+
no_argument(1) // BAD: `no_argument` will accept and ignore the argument
7+
8+
one_argument(1); // GOOD: `one_argument` will accept and use the argument
9+
10+
no_argument(); // GOOD: `no_argument` has not been passed an argument
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>A function is called with arguments despite having an empty parameter list. This may indicate
9+
that the incorrect function is being called, or that the author misunderstood the function.</p>
10+
11+
</overview>
12+
<recommendation>
13+
<p>Call the function without arguments, or call a different function that expects the arguments
14+
being passed.</p>
15+
16+
</recommendation>
17+
<example><sample src="FutileParams.c" />
18+
19+
</example>
20+
21+
<references>
22+
<li>SEI CERT C++ Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/DCL20-C.+Explicitly+specify+void+when+a+function+accepts+no+arguments"> DCL20-C. Explicitly specify void when a function accepts no arguments </a></li>
23+
</references>
24+
</qhelp>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Non-empty call to function declared without parameters
3+
* @description A call to a function declared without parameters has arguments, which may indicate
4+
* that the code does not follow the author's intent.
5+
* @kind problem
6+
* @problem.severity warning
7+
*/
8+
9+
import cpp
10+
11+
from Function f, FunctionCall fc
12+
where fc.getTarget() = f
13+
and f.getNumberOfParameters() = 0
14+
and not f.isVarargs()
15+
and fc.getNumberOfArguments() != 0
16+
and not f instanceof BuiltInFunction
17+
and exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() | not fde.isImplicit())
18+
select fc, "This call has arguments, but $@ is not declared with any parameters.", f, f.toString()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:7:3:7:5 | call to foo | This call has arguments, but $@ is not declared with any parameters. | test.c:1:6:1:8 | foo | foo |
2+
| test.c:13:3:13:19 | call to not_yet_declared1 | This call has arguments, but $@ is not declared with any parameters. | test.c:13:3:13:3 | not_yet_declared1 | not_yet_declared1 |
3+
| test.c:13:3:13:19 | call to not_yet_declared1 | This call has arguments, but $@ is not declared with any parameters. | test.c:17:6:17:22 | not_yet_declared1 | not_yet_declared1 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Likely Bugs/Likely Typos/FutileParams.ql
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
void foo();
2+
void bar(void);
3+
void baz(int);
4+
5+
void test() {
6+
foo(); // GOOD
7+
foo(1); // BAD
8+
bar(); // GOOD
9+
baz(1); // GOOD
10+
11+
undeclared(1); // GOOD
12+
13+
not_yet_declared1(1); // BAD
14+
not_yet_declared2(1); // GOOD
15+
}
16+
17+
void not_yet_declared1();
18+
void not_yet_declared2(int);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
void cpp_varargs(...);
2+
void bar();
3+
4+
void test() {
5+
cpp_varargs(); // GOOD
6+
cpp_varargs(1); // GOOD
7+
__builtin_constant_p("something"); // GOOD: builtin
8+
}

0 commit comments

Comments
 (0)