ISO/IEC TS 17961 [argcomp]
Calling functions with incorrect arguments
Description
Rule Definition
Calling functions with incorrect arguments.1
Polyspace Implementation
This checker checks for these issues:
Conflicting declarations or conflicting declaration and definition.
Unreliable cast of function pointer.
Examples
Conflicting declarations or conflicting declaration and definition
The issue occurs when all declarations of an object or function do not use the same names and type qualifiers.
The rule checker detects situations where parameter names or data types are different between multiple declarations or the declaration and the definition. The checker considers declarations in all translation units and flags issues that are not likely to be detected by a compiler.
The checker does not flag this issue in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
Consistently using parameter names and types across declarations of the same object or function encourages stronger typing. It is easier to check that the same function interface is used across all declarations.
extern int div (int num, int den); int div(int den, int num) { /* Non compliant */ return(num/den); }
In this example, the rule is violated because the parameter names in the declaration and definition are switched.
typedef unsigned short width; typedef unsigned short height; typedef unsigned int area; extern area calculate(width w, height h); area calculate(width w, width h) { /* Noncompliant */ return w*h; }
In this example, the rule is violated
because the second argument of the calculate
function has
data type:
height
in the declaration.width
in the definition.
The rule is violated even though the underlying type of
height
and width
are identical.
Unreliable cast of function pointer
Unreliable cast of function pointer occurs when a function pointer is cast to another function pointer that has different argument or return type.
This defect applies only if the code language for the project is C.
If you cast a function pointer to another function pointer with different argument or return type and then use the latter function pointer to call a function, the behavior is undefined.
Avoid a cast between two function pointers with mismatch in argument or return types.
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#define PI 3.142
double Calculate_Sum(int (*fptr)(double))
{
double sum = 0.0;
double y;
for (int i = 0; i <= 100; i++)
{
y = (*fptr)(i*PI/100);
sum += y;
}
return sum / 100;
}
int main(void)
{
double (*fp)(double);
double sum;
fp = sin;
sum = Calculate_Sum(fp);
/* Defect: fp implicitly cast to int(*) (double) */
printf("sum(sin): %f\n", sum);
return 0;
}
The function pointer fp
is
declared as double (*)(double)
. However in passing
it to function Calculate_Sum
, fp
is
implicitly cast to int (*)(double)
.
One possible correction is to check that the
function pointer in the definition of Calculate_Sum
has
the same argument and return type as fp
. This step
makes sure that fp
is not implicitly cast to a
different argument or return type.
#include <stdio.h> #include <math.h> #include <stdio.h> # define PI 3.142 /*Fix: fptr has same argument and return type everywhere*/ double Calculate_Sum(double (*fptr)(double)) { double sum = 0.0; double y; for (int i = 0; i <= 100; i++) { y = (*fptr)(i*PI/100); sum += y; } return sum / 100; } int main(void) { double (*fp)(double); double sum; fp = sin; sum = Calculate_Sum(fp); printf("sum(sin): %f\n", sum); return 0; }
Check Information
Decidability: Undecidable |
Version History
Introduced in R2019a
1 Extracts from the standard "ISO/IEC TS 17961 Technical Specification - 2013-11-15" are reproduced with the agreement of AFNOR. Only the original and complete text of the standard, as published by AFNOR Editions - accessible via the website www.boutique.afnor.org - has normative value.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)