请教C语言函数中有多个return的安全隐患?
在著名的Misra C编码标准里,有这么一条规则:
A function shall have a single point of exit at the end of the function.
也就是说,一个函数只能在函数的末尾出现一个出口,如果在一个函数中使用多个return是不允许的。如下面的例子是不允许的:
int function(parameter list)
{
If(expression1)
{
Statement sequence;
return code_1;
}
If(expression2)
{
Statement sequence;
return code_2;
}
If(expression3)
{
Statement sequence;
return code_3;
}
return code_4;
}
需要改成:
int function(parameter list)
{
If(expression1)
{
Statement sequence;
Return_val= code_1;
}
If(expression2)
{
Statement sequence;
Return_val=code_2;
}
If(expression3)
{
Statement sequence;
Return_val=code_3;
}
return Return_val;
}
请教各位大虾,C语言函数中有多个return有什么安全隐患?我觉得没什么啊。。。