How does polyspace bug finder intelligently identify useless if errors in if-else

8 visualizaciones (últimos 30 días)
The if-ESLE condition in the source code is the user's input, and according to the input value, it is decided which if branch to execute. The condition will have an initial value, and the bug finder will conclude that all other branches that do not satisfy the condition are useless if.How to avoid not reporting a useless if error in this case
For example:
#define value 5
int main(){
if (value == 5 )
printf("if condition")
else:
printf("useless if") // bug finder will determine that this branch is useless if
}
}

Respuestas (1)

Shubham
Shubham el 6 de Sept. de 2023
Hi,
I understand you want to avoid the useless check flagged by Polyspace.
The reason Polyspace is marking the variable 'value' as useless is because you have used the 'define' macro to fix its value as 5. By definition, the 'define' macro assigns a constant value to a variable, which cannot be changed during execution. Therefore, Polyspace considers 'else' as useless because it will never be executed because the value of variable 'value' will always be 5.
If you want 'val' to be a user input that can vary during execution, then you should not use a macro to assign its value. Instead, you can use a different approach to allow the user to input the value of 'val' dynamically, such as using the 'scanf' function.
I hope this clarifies the reason behind Polyspace marking 'else' as useless.
  2 comentarios
玫青
玫青 el 6 de Sept. de 2023
First of all, thank you for your answer, but our source code belongs to embedded software, so it is not suitable to use "scanf" to obtain user input.
In addtion, I would like to know whether poly space supports custom defect rules? Can I customize the rule of useless if?
Shubham
Shubham el 6 de Sept. de 2023
Hi, you can use your preffered way of taking an input, but you should avoid "#define" to assign the value if it will be changed during execution.
Additionally Polyspace does not allow customization of defect rules but you can suppress the acceptable defects by adding code annotations. Check this documentation for more info:Annotate Code and Hide Known or Acceptable Results - MATLAB & Simulink (mathworks.com)

Iniciar sesión para comentar.

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by