MATLAB Answers

How to give data ranges to function parameters which are pointers

11 views (last 30 days)
Nideesh Madhu
Nideesh Madhu on 6 May 2021
Commented: Anirban on 7 May 2021
While running code prover I got so many Orange warnings and was trying to reduce them by giving data ranges
to function input parameters. But I was not able to input the range for pointer variables.(There is some structure pointer
passed to our functions).
Is there any way of giving data range to pointer variables?
For example in the below code snippet how to give data ranges for a->x,a->y and a->z
typedef volatile struct
{
uint32_t x;
uint32_t y;
uint32_t z;
} GTM_XYZ_Pram_T;
void DD_GTM_XYZ_Init ( GTM_XYZ_Pram_T *a, uint32_t b, uint32_t c, uint32_t d )
{
}

Accepted Answer

Anirban
Anirban on 6 May 2021
I assume you are getting an orange warning on the dereferences a->x, a->y, and so on. The orange warning is probably an Illegally dereferenced pointer warning that states that a might be NULL.
You can specify a constraint that the pointer a is not NULL (and also other constraints). See more details in External Constraints for Polyspace Analysis. This constraint is not available in C++ since a base type pointer can point to derived types and managing constraints on pointers is difficult. It is possible that your Source code language is set to C-CPP, so you are running into the C++ limitation. If you are using a C-only project, you can set the language to C. Then in the Constraint Specification interface, you should be able to constrain pointers.
  4 Comments
Anirban
Anirban on 7 May 2021
For the sake of completeness, I also wanted to answer your question on constraining structure fields through a pointer. Although you cannot constrain it directly through the Constraint Specification wizard yet, you can impose a constraint as follows.
Let us say you want to constrain the values of a->x, a->y and a->z here (note that I removed volatile from your example, otherwise constraint specifications won't apply, see Assumptions About Volatile Variables):
typedef struct
{
uint32_t x;
uint32_t y;
uint32_t z;
} GTM_XYZ_Pram_T;
void DD_GTM_XYZ_Init ( GTM_XYZ_Pram_T *a, uint32_t b, uint32_t c, uint32_t d )
{
}
You can write a function data_constraints that constrains those parameters and then passes the constrained parameters onto your real function. Like this:
void data_constraints( GTM_XYZ_Pram_T *a, uint32_t b, uint32_t c, uint32_t d) {
assert(a->x >=0 && a->x <=255);
assert(a->y >=0 && a->y <=255);
assert(a->z >=0 && a->z <=255);
DD_GTM_XYZ_Init(a, b, c, d);
}
You can then provide the file containing the function data_constraints for the Polyspace analysis. If you contact Technical Support, they can help you with specifying data constraints this way.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by