필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
玫青
玫青 2023년 9월 6일
댓글: Shubham 2023년 9월 6일
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)

댓글을 달려면 로그인하십시오.

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by