himl_0011: Data type and size of condition expressions

ID: Titlehiml_0011: Data type and size of condition expressions
Description

Logical scalars should be used for condition expressions. Condition expressions include:

  • if expressions

  • elseif expressions

  • while expressions

  • Condition expressions of Stateflow® transitions

Rationale

Prevent execution of unexpected code paths

Model Advisor Checks
  • By Task > Modeling Standards for DO-178C/DO-331 > High-Integrity Systems > MATLAB > Check type and size of condition expressions

  • By Task > Modeling Standards for IEC 61508 > High-Integrity Systems > MATLAB > Check type and size of condition expressions

  • By Task > Modeling Standards for IEC 62304 > High-Integrity Systems > MATLAB > Check type and size of condition expressions

  • By Task > Modeling Standards for EN 50128 > High-Integrity Systems > MATLAB > Check type and size of condition expressions

  • By Task > Modeling Standards for ISO 26262 > High-Integrity Systems > MATLAB > Check type and size of condition expressions

For check details, see Check type and size of condition expressions (Simulink Check).

References
  • IEC 61508-3, Table A.3 (2) 'Strongly typed programming language’
    IEC 61508-3, Table A.3 (3) 'Language subset’

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1(b) 'Use of language subsets'
    ISO 26262-6, Table 1(c) 'Enforcement of strong typing'

  • EN 50128, Table A.4 (8) 'Strongly Typed Programming Language'
    EN 50128, Table A.4 (11) 'Language Subset'

  • DO-331, Section MB.6.3.1.g 'Algorithms are accurate'
    DO-331, Section MB.6.3.2.g 'Algorithms are accurate'

  • MISRA C:2012 Rule 14.4 - The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essential Boolean type.

Last ChangedR2019b
Examples

Recommended

Assume variable var is a scalar of type double with value -1.

MATLAB Code:

if var > 0 % expression is a logical scalar
    … % will not be executed
elseif var < 0 % expression is a logical scalar
    … % will be executed
else
    … % will not be executed
end
while var < 5 % expression is a logical scalar        
    var = var + 1; % executed 5 times
end

Stateflow Transition Condition:

[var > 0]{…} % condition action will not be executed

Not Recommended

Assume variable var is a scalar of type double with value -1.

MATLAB Code:

if var % expression is a double scalar
    … % will be executed because var is non-zero
elseif ~var    
    … % will not be executed
else
    … % will not be executed
end
while var % expression is a double scalar
    var = var + 1; % executed 1 time
end

Stateflow Transition Condition:

[var]{…} % condition action will be executed because var is non-zero