Skip to content

Commit 54091e8

Browse files
authored
Merge pull request #1136 from zlaski-semmle/cpp340a
[CPP-340] Refinements to FutileParams.ql etc.
2 parents 3f70d91 + 17066cf commit 54091e8

26 files changed

+476
-97
lines changed

change-notes/1.21/analysis-cpp.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
| **Query** | **Tags** | **Purpose** |
88
|-----------------------------|-----------|--------------------------------------------------------------------|
9+
| `()`-declared function called with too few arguments (`cpp/too-few-arguments`) | Correctness | Find all cases where the number of arguments is less than the number of parameters of the function, provided the function is also properly declared/defined elsewhere. |
10+
| `()`-declared function called with mismatched arguments (`cpp/mismatched-function-arguments`) | Correctness | Find all cases where the types of arguments do not match the types of parameters of the function, provided the function is also properly declared/defined elsewhere. |
911

1012
## Changes to existing queries
1113

@@ -24,6 +26,7 @@
2426
| Use of potentially dangerous function | More correct results | Calls to `localtime`, `ctime` and `asctime` are now detected by this query. |
2527
| Wrong type of arguments to formatting function (`cpp/wrong-type-format-argument`) | More correct results and fewer false positive results | This query now more accurately identifies wide and non-wide string/character format arguments on different platforms. Platform detection has also been made more accurate for the purposes of this query. |
2628
| Wrong type of arguments to formatting function (`cpp/wrong-type-format-argument`) | Fewer false positive results | Non-standard uses of %L are now understood. |
29+
| `()`-declared function called with too many arguments (`cpp/futile-params`) | Improved coverage | Query has been generalized to find all cases where the number of arguments exceedes the number of parameters of the function, provided the function is also properly declared/defined elsewhere. |
2730

2831
## Changes to QL libraries
2932
- Additional support for definition by reference has been added to the `semmle.code.cpp.dataflow.TaintTracking` library.

cpp/config/suites/c/correctness

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
+ semmlecode-cpp-queries/Likely Bugs/Arithmetic/IntMultToLong.ql: /Correctness/Dangerous Conversions
77
+ semmlecode-cpp-queries/Likely Bugs/Conversion/NonzeroValueCastToPointer.ql: /Correctness/Dangerous Conversions
88
+ semmlecode-cpp-queries/Likely Bugs/Conversion/ImplicitDowncastFromBitfield.ql: /Correctness/Dangerous Conversions
9+
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/MistypedFunctionArguments.ql: /Correctness/Dangerous Conversions
910
+ semmlecode-cpp-queries/Security/CWE/CWE-253/HResultBooleanConversion.ql: /Correctness/Dangerous Conversions
1011
# Consistent Use
1112
+ semmlecode-cpp-queries/Critical/ReturnValueIgnored.ql: /Correctness/Consistent Use
@@ -15,7 +16,8 @@
1516
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/AssignWhereCompareMeant.ql: /Correctness/Common Errors
1617
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/CompareWhereAssignMeant.ql: /Correctness/Common Errors
1718
+ 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
19+
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/TooFewArguments.ql: /Correctness/Common Errors
20+
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/TooManyArguments.ql: /Correctness/Common Errors
1921
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/ShortCircuitBitMask.ql: /Correctness/Common Errors
2022
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/MissingEnumCaseInSwitch.ql: /Correctness/Common Errors
2123
+ semmlecode-cpp-queries/Likely Bugs/Arithmetic/FloatComparison.ql: /Correctness/Common Errors

cpp/config/suites/cpp/correctness

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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/Likely Bugs/Conversion/CastArrayPointerArithmetic.ql: /Correctness/Dangerous Conversions
10+
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/MistypedFunctionArguments.ql: /Correctness/Dangerous Conversions
1011
+ semmlecode-cpp-queries/Security/CWE/CWE-253/HResultBooleanConversion.ql: /Correctness/Dangerous Conversions
1112
# Consistent Use
1213
+ semmlecode-cpp-queries/Critical/ReturnValueIgnored.ql: /Correctness/Consistent Use
@@ -16,7 +17,8 @@
1617
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/AssignWhereCompareMeant.ql: /Correctness/Common Errors
1718
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/CompareWhereAssignMeant.ql: /Correctness/Common Errors
1819
+ 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
20+
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/TooFewArguments.ql: /Correctness/Common Errors
21+
+ semmlecode-cpp-queries/Likely Bugs/Underspecified Functions/TooManyArguments.ql: /Correctness/Common Errors
2022
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/ShortCircuitBitMask.ql: /Correctness/Common Errors
2123
+ semmlecode-cpp-queries/Likely Bugs/Likely Typos/MissingEnumCaseInSwitch.ql: /Correctness/Common Errors
2224
+ semmlecode-cpp-queries/Likely Bugs/Arithmetic/FloatComparison.ql: /Correctness/Common Errors

cpp/ql/src/Likely Bugs/Likely Typos/FutileParams.c

Lines changed: 0 additions & 11 deletions
This file was deleted.

cpp/ql/src/Likely Bugs/Likely Typos/FutileParams.qhelp

Lines changed: 0 additions & 29 deletions
This file was deleted.

cpp/ql/src/Likely Bugs/Likely Typos/FutileParams.ql

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void three_arguments(int x, int y, int z);
2+
3+
void calls() {
4+
int three = 3;
5+
three_arguments(1, 2, three); // GOOD
6+
three_arguments(1, 2, &three); // BAD
7+
}
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 at least one argument whose type is incompatible with the type of
9+
the corresponding parameter of the function being called. This may cause the called function
10+
to behave unpredictably.</p>
11+
12+
<p>This may indicate that an incorrect function is being called, or that the
13+
signature (parameter list and parameter types) of the called function
14+
is not known to the author.</p>
15+
16+
</overview>
17+
<recommendation>
18+
<p>Call the function with the proper argument types. In some cases, it may
19+
suffice to provide an explicit cast of an argument to the desired (parameter) type.</p>
20+
21+
</recommendation>
22+
<example><sample src="MistypedFunctionArguments.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: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @name Call to a function with one or more incompatible arguments
3+
* @description When the type of a function argument is not compatible
4+
* with the type of the corresponding parameter, it may lead to
5+
* unpredictable behavior.
6+
* @kind problem
7+
* @problem.severity warning
8+
* @precision medium
9+
* @id cpp/mistyped-function-arguments
10+
* @tags correctness
11+
* maintainability
12+
*/
13+
14+
import cpp
15+
16+
predicate arithTypesMatch(Type arg, Type parm) {
17+
arg = parm
18+
or
19+
arg.getSize() = parm.getSize() and
20+
(
21+
arg instanceof IntegralOrEnumType and
22+
parm instanceof IntegralOrEnumType
23+
or
24+
arg instanceof FloatingPointType and
25+
parm instanceof FloatingPointType
26+
)
27+
}
28+
29+
pragma[inline]
30+
predicate nestedPointerArgTypeMayBeUsed(Type arg, Type parm) {
31+
// arithmetic types
32+
arithTypesMatch(arg, parm)
33+
or
34+
// conversion to/from pointers to void is allowed
35+
arg instanceof VoidType
36+
or
37+
parm instanceof VoidType
38+
}
39+
40+
pragma[inline]
41+
predicate pointerArgTypeMayBeUsed(Type arg, Type parm) {
42+
nestedPointerArgTypeMayBeUsed(arg, parm)
43+
or
44+
// nested pointers
45+
nestedPointerArgTypeMayBeUsed(arg.(PointerType).getBaseType().getUnspecifiedType(),
46+
parm.(PointerType).getBaseType().getUnspecifiedType())
47+
or
48+
nestedPointerArgTypeMayBeUsed(arg.(ArrayType).getBaseType().getUnspecifiedType(),
49+
parm.(PointerType).getBaseType().getUnspecifiedType())
50+
}
51+
52+
pragma[inline]
53+
predicate argTypeMayBeUsed(Type arg, Type parm) {
54+
// arithmetic types
55+
arithTypesMatch(arg, parm)
56+
or
57+
// pointers to compatible types
58+
pointerArgTypeMayBeUsed(arg.(PointerType).getBaseType().getUnspecifiedType(),
59+
parm.(PointerType).getBaseType().getUnspecifiedType())
60+
or
61+
pointerArgTypeMayBeUsed(arg.(ArrayType).getBaseType().getUnspecifiedType(),
62+
parm.(PointerType).getBaseType().getUnspecifiedType())
63+
or
64+
// C11 arrays
65+
pointerArgTypeMayBeUsed(arg.(PointerType).getBaseType().getUnspecifiedType(),
66+
parm.(ArrayType).getBaseType().getUnspecifiedType())
67+
or
68+
pointerArgTypeMayBeUsed(arg.(ArrayType).getBaseType().getUnspecifiedType(),
69+
parm.(ArrayType).getBaseType().getUnspecifiedType())
70+
}
71+
72+
// This predicate holds whenever expression `arg` may be used to initialize
73+
// function parameter `parm` without need for run-time conversion.
74+
pragma[inline]
75+
predicate argMayBeUsed(Expr arg, Parameter parm) {
76+
argTypeMayBeUsed(arg.getFullyConverted().getType().getUnspecifiedType(),
77+
parm.getType().getUnspecifiedType())
78+
}
79+
80+
// True if function was ()-declared, but not (void)-declared or K&R-defined
81+
predicate hasZeroParamDecl(Function f) {
82+
exists(FunctionDeclarationEntry fde | fde = f.getADeclarationEntry() |
83+
not fde.hasVoidParamList() and fde.getNumberOfParameters() = 0 and not fde.isDefinition()
84+
)
85+
}
86+
87+
// True if this file (or header) was compiled as a C file
88+
predicate isCompiledAsC(Function f) {
89+
exists(File file | file.compiledAsC() |
90+
file = f.getFile() or file.getAnIncludedFile+() = f.getFile()
91+
)
92+
}
93+
94+
from FunctionCall fc, Function f, Parameter p
95+
where
96+
f = fc.getTarget() and
97+
p = f.getAParameter() and
98+
hasZeroParamDecl(f) and
99+
isCompiledAsC(f) and
100+
not f.isVarargs() and
101+
not f instanceof BuiltInFunction and
102+
p.getIndex() < fc.getNumberOfArguments() and
103+
// Parameter p and its corresponding call argument must have mismatched types
104+
not argMayBeUsed(fc.getArgument(p.getIndex()), p)
105+
select fc, "Calling $@: argument $@ of type $@ is incompatible with parameter $@.", f, f.toString(),
106+
fc.getArgument(p.getIndex()) as arg, arg.toString(),
107+
arg.getExplicitlyConverted().getType().getUnspecifiedType() as atype, atype.toString(), p,
108+
p.getTypedName()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void one_argument();
2+
3+
void calls() {
4+
one_argument(1); // GOOD: `one_argument` will accept and use the argument
5+
6+
one_argument(); // BAD: `one_argument` will receive an undefined value
7+
}
8+
9+
void one_argument(int x);

0 commit comments

Comments
 (0)