Correctness condition
Mismatch occurs during pointer cast or function pointer use
Description
This check determines whether:
An array is mapped to a larger array through a pointer cast
A function pointer points to a function with a valid prototype
A global variable falls outside the range specified through the Global Assert mode. See also Constrain Global Variable Range for Polyspace Analysis.
Diagnosing This Check
Examples
Array Mapped to Larger Array
typedef int smallArray[10]; typedef int largeArray[100]; void main(void) { largeArray myLargeArray; smallArray *smallArrayPtr = (smallArray*) &myLargeArray; largeArray *largeArrayPtr = (largeArray*) smallArrayPtr; }
In this example:
In the first pointer cast, a pointer of type
largeArray
is cast to a pointer of typesmallArray
. Because the data typesmallArray
represents a smaller array, the Correctness condition check is green.In the second pointer cast, a pointer of type
smallArray
is cast to a pointer of typelargeArray
. Because the data typelargeArray
represents a larger array, the Correctness condition check is red.
Function Pointer Is Null
typedef void (*callBack) (float data);
typedef struct
{
char funcName[20];
callBack func;
} funcStruct;
funcStruct myFuncStruct;
void main(void)
{
float val = 0.f;
myFuncStruct.func = (void*)0;
myFuncStruct.func(val);
}
In this example, the function pointer myFuncStruct.func
is initialized with NULL
before use. When the pointer is dereferenced, the Correctness condition check produces a red error.
Function Pointer Does Not Point to Function
typedef void (*callBack) (float data);
typedef struct {
char funcName[20];
callBack func;
} funcStruct;
funcStruct myFuncStruct;
void main(void) {
float val = 0.f;
myFuncStruct.func(val);
}
In this example, the global variable myFuncStruct
is not initialized, so the function pointer myFuncStruct.func
contains NULL
. When the pointer myFuncStruct.func
is dereferenced, the Correctness condition check produces a red error.
Function Pointer Points to Function via Absolute Address Usage
#define MAX_MEMSEG 32764
typedef void (*ptrFunc)(int memseg);
ptrFunc operation = (ptrFunc)(0x003c);
void main(void) {
for (int i=1; i <= MAX_MEMSEG; i++)
operation(i);
}
In this example, the function pointer operation
is
cast to the contents of a memory location. Polyspace® cannot determine
whether the location contains a variable or a function code and whether
the function is well-typed. Therefore, when the pointer operation
is
dereferenced and used in a function call, the Correctness
condition check is orange.
After an orange Correctness condition check due to absolute address usage, the software assumes that the following variables have the full range of values allowed by their type:
Variable storing the return value from the function call.
In the following example, the software assumes that the return value of
operation
is full-range.typedef int (*ptrFunc)(int); ptrFunc operation = (ptrFunc)(0x003c); int main(void) { return operation(0); }
Variables that can be modified through the function arguments.
In the following example, the function pointer
operation
takes a pointer argumentptr
that points to a variablevar
. After the call tooperation
, the software assumes thatvar
has full-range value.typedef void (*ptrFunc)(int*); ptrFunc operation = (ptrFunc)(0x003c); void main(void) { int var; int *ptr=&var; operation(ptr); }
Pointer Points to Function Pointer
typedef void (*callBack) (float data);
typedef struct {
char funcName[20];
callBack func;
} funcStruct;
void g(float data);
funcStruct FctPtrLUT[2] = {
{"test",g},
{"test",g}
};
void main(void) {
float val = 0.f;
funcStruct* cb;
cb=FctPtrLUT[0].func;
cb->func(val);
}
In this example, the code declares cb
as a pointer to
funcStruct
and, in the last line of the
main
function, dereferences cb
as if it
were a pointer to funcStruct
with access to the field
func
.
However, the code actually assigns cb
to
FctPtrLUT[0].func
, which points to the address of the
function g()
.
Because the expected type of cb
(funcStruct*
) and the actual type (the function pointer type callback
) do not match, Polyspace is unable to find a correct function call and assigns the finding an orange Correctness condition check. This mismatch in type causes a compilation error in C++ code but might not be detected at compile time in C code.
Function Pointer Points to Function with Wrong Argument Type
typedef struct {
double real;
double imag;
} complex;
typedef int (*typeFuncPtr) (complex*);
int func(int* x);
void main() {
typeFuncPtr funcPtr = (typeFuncPtr)&func;
int arg = 0, result = funcPtr((complex*)&arg);
}
In this example, the function pointer funcPtr
points
to a function with argument type complex*
. However,
the pointer is assigned the address of function func
whose
argument type is int*
. Because of this type mismatch,
the Correctness condition check is orange.
Function Pointer Points to Function with Wrong Number of Arguments
typedef int (*typeFuncPtr) (int, int);
int func(int);
void main() {
typeFuncPtr funcPtr = (typeFuncPtr)&func;
int arg1 = 0, arg2 = 0, result = funcPtr(arg1,arg2);
}
In this example, the function pointer funcPtr
points
to a function with two int
arguments. However,
it is assigned the function func
which has one int
argument
only. Because of this mismatch in number of arguments, the Correctness
condition check is orange.
Function Pointer Points to Function with Wrong Return Type
typedef double (*typeFuncPtr) (int);
int func(int);
void main() {
typeFuncPtr funcPtr = (typeFuncPtr)&func;
int arg = 0;
double result = funcPtr(arg);
}
In this example, the function pointer funcPtr
points
to a function with return type double
. However,
it is assigned the function func
whose return type
is int
. Because of this mismatch in return types,
the Correctness condition check is orange.
Variable Falls Outside Global Assert Range
int glob = 0; int func(); void main() { glob = 5; glob = func(); glob+= 20; }
In this example, a range of 0..10
was specified
for the global variable glob
.
In the statement
glob=5;
, a green Correctness condition check appears onglob
.In the statement
glob=func();
, an orange Correctness condition check appears onglob
because the return value of stubbed functionfunc
can be outside0..10
.After this statement, Polyspace considers that
glob
has values in0..10
.In the statement
glob+=20;
, a red Correctness condition check appears onglob
because after the addition,glob
has values in20..30
.
See also Constrain Global Variable Range for Polyspace Analysis.
Check Information
Group: Other |
Language: C | C++ |
Acronym: COR |
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 (한국어)