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

Skip to content

Commit bf64fee

Browse files
authored
Merge pull request #790 from rdmarsh2/rdmarsh/cpp/futile-params
Approved by semmledocs-ac
2 parents 15643d1 + 9642a78 commit bf64fee

9 files changed

Lines changed: 107 additions & 2 deletions

File tree

cpp/config/suites/c/correctness

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
+ semmlecode-cpp-queries/Likely Bugs/Conversion/NonzeroValueCastToPointer.ql: /Correctness/Dangerous Conversions
88
+ semmlecode-cpp-queries/Likely Bugs/Conversion/ImplicitDowncastFromBitfield.ql: /Correctness/Dangerous Conversions
99
+ semmlecode-cpp-queries/Security/CWE/CWE-253/HResultBooleanConversion.ql: /Correctness/Dangerous Conversions
10-
# Consistent Use
10+
# Consistent Use
1111
+ semmlecode-cpp-queries/Critical/ReturnValueIgnored.ql: /Correctness/Consistent Use
1212
+ semmlecode-cpp-queries/Likely Bugs/InconsistentCheckReturnNull.ql: /Correctness/Consistent Use
1313
+ semmlecode-cpp-queries/Likely Bugs/InconsistentCallOnResult.ql: /Correctness/Consistent Use
1414
# Common Errors
1515
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/AssignWhereCompareMeant.ql: /Correctness/Common Errors
1616
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/CompareWhereAssignMeant.ql: /Correctness/Common Errors
1717
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/ExprHasNoEffect.ql: /Correctness/Common Errors
18+
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/FutileParams.ql: /Correctness/Common Errors
1819
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/ShortCircuitBitMask.ql: /Correctness/Common Errors
1920
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/MissingEnumCaseInSwitch.ql: /Correctness/Common Errors
2021
+ semmlecode-cpp-queries/Likely Bugs/Arithmetic/FloatComparison.ql: /Correctness/Common Errors

cpp/config/suites/cpp/correctness

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
+ semmlecode-cpp-queries/Likely Bugs/Conversion/ImplicitDowncastFromBitfield.ql: /Correctness/Dangerous Conversions
99
+ semmlecode-cpp-queries/Likely Bugs/Conversion/CastArrayPointerArithmetic.ql: /Correctness/Dangerous Conversions
1010
+ semmlecode-cpp-queries/Security/CWE/CWE-253/HResultBooleanConversion.ql: /Correctness/Dangerous Conversions
11-
# Consistent Use
11+
# Consistent Use
1212
+ semmlecode-cpp-queries/Critical/ReturnValueIgnored.ql: /Correctness/Consistent Use
1313
+ semmlecode-cpp-queries/Likely Bugs/InconsistentCheckReturnNull.ql: /Correctness/Consistent Use
1414
+ semmlecode-cpp-queries/Likely Bugs/InconsistentCallOnResult.ql: /Correctness/Consistent Use
1515
# Common Errors
1616
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/AssignWhereCompareMeant.ql: /Correctness/Common Errors
1717
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/CompareWhereAssignMeant.ql: /Correctness/Common Errors
1818
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/ExprHasNoEffect.ql: /Correctness/Common Errors
19+
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/FutileParams.ql: /Correctness/Common Errors
1920
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/ShortCircuitBitMask.ql: /Correctness/Common Errors
2021
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/MissingEnumCaseInSwitch.ql: /Correctness/Common Errors
2122
+ semmlecode-cpp-queries/Likely Bugs/Arithmetic/FloatComparison.ql: /Correctness/Common Errors
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
<p>In C, a function declared with an empty parameter list `()` is considered to have an unknown
12+
parameter list, and therefore can be called with any set of arguments. To declare a function
13+
which takes no arguments, you must use `(void)` as the parameter list in any forward declarations.
14+
In C++, either style of declaration indicates that the function accepts no arguments.</p>
15+
16+
</overview>
17+
<recommendation>
18+
<p>Call the function without arguments, or call a different function that expects the arguments
19+
being passed.</p>
20+
21+
</recommendation>
22+
<example><sample src="FutileParams.c" />
23+
24+
</example>
25+
26+
<references>
27+
<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>
28+
</references>
29+
</qhelp>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* @precision very-high
8+
* @id cpp/futile-params
9+
* @tags correctness
10+
* maintainability
11+
*/
12+
13+
import cpp
14+
15+
from Function f, FunctionCall fc
16+
where fc.getTarget() = f
17+
and f.getNumberOfParameters() = 0
18+
and not f.isVarargs()
19+
and fc.getNumberOfArguments() != 0
20+
and not f instanceof BuiltInFunction
21+
and exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() | not fde.isImplicit())
22+
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:8:3:8:16 | call to declared_empty | This call has arguments, but $@ is not declared with any parameters. | test.c:1:6:1:19 | declared_empty | declared_empty |
2+
| test.c:14:3:14:19 | call to not_yet_declared1 | This call has arguments, but $@ is not declared with any parameters. | test.c:14:3:14:3 | not_yet_declared1 | not_yet_declared1 |
3+
| test.c:14:3:14:19 | call to not_yet_declared1 | This call has arguments, but $@ is not declared with any parameters. | test.c:25:6:25: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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
void declared_empty();
2+
void declared_void(void);
3+
void declared_with(int);
4+
void declared_empty_defined_with();
5+
6+
void test() {
7+
declared_empty(); // GOOD
8+
declared_empty(1); // BAD
9+
declared_void(); // GOOD
10+
declared_with(1); // GOOD
11+
12+
undeclared(1); // GOOD
13+
14+
not_yet_declared1(1); // BAD
15+
not_yet_declared2(1); // GOOD
16+
17+
declared_empty_defined_with(); // BAD [NOT DETECTED]
18+
declared_empty_defined_with(1); // GOOD
19+
20+
int x;
21+
declared_empty_defined_with(&x); // BAD [NOT DETECTED]
22+
declared_empty_defined_with(x, x); // BAD [NOT DETECTED]
23+
}
24+
25+
void not_yet_declared1();
26+
void not_yet_declared2(int);
27+
void declared_empty_defined_with(int x) {
28+
// do nothing
29+
}
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)