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

9 次查看(过去 30 天)
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
}
}

回答(1 个)

Shubham
Shubham 2023-9-6
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 个评论
玫青
玫青 2023-9-6
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 2023-9-6
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by