主要内容

hisl_0029: Usage of Assignment blocks

R2026b

Initialize array fields before first use in the Assignment block to avoid undesirable results in generated code

Usage: High-Integrity System Modeling

Guideline ID: hisl_0029

Rules

hisl_0029: Usage of Assignment blocks

To support robustness of generated code, when using the Assignment block, initialize array fields before their first use.

Rationale

Avoid undesirable results in generated code.

Verification

Check usage of Assignment blocks (Simulink Check)

Example — Correct

Initialization input Y0 when block is not used iteratively.

Simulink model with a constant [0 0 0] connected to Y0 input and two inports connected to the U and Idx inputs of a 1-D Assignment block.

Generated code initializing all three elements of the output array to 0.0 before assigning the indexed value.

Example — Correct

Initialize array fields when block is used iteratively.

Simulink model using a For Iterator subsystem with an Add block feeding the U input of a 1-D Assignment block, with the iterator index connected to Idx.

Generated code using a for loop to assign values to each array element iteratively without requiring prior initialization.

Example — Incorrect

No initialization input Y0 when block is not used iteratively.

Simulink model with a 1-D Assignment block receiving only the U input and Idx from a Constant block, with no Y0 initialization input connected.

Generated code declaring the output array without initialization, leaving element [1] uninitialized before it is read.

Tips

  • If the output vector of the Assignment block is not initialized with an input to the block, elements of the vector might not be initialized in the generated code.

  • When the Assignment block is used iteratively and array fields are assigned during one simulation time step, you do not need initialization input to the block.

  • Accessing uninitialized elements of block output can result in unexpected behavior.

  • For a partial write operations, maintain a persistent output buffer (for example, see cgsl_0408: Partial data send for component deployment).

Industry Standards

  • IEC 61508-3, Table A.3 (3) 'Language subset'

    IEC 61508-3, Table A.4 (3) 'Defensive programming'

    IEC 61508-3, Table A.3 (2) 'Strongly typed programming language'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262–6, Table 1(b) 'Use of language subsets'

    ISO 26262–6, Table 1(d) 'Use of defensive implementation techniques'

  • EN 50128, Table A.4 (11) 'Language Subset'

    EN 50128, Table A.3 (1) 'Defensive Programming'

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

  • EN 50657, Table A.4 (11) 'Language Subset'

    EN 50657, Table A.3 (1) 'Defensive Programming'

    EN 50657, Table A.4 (8) 'Strongly Typed Programming Language'

  • EN 50716, Table A.3 (1) 'Defensive Programming'

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

  • MISRA C:2012, Rule 9.1

  • MISRA C:2023, Rule 9.1

  • EXP33-C. Do not read uninitialized memory

Version History

Introduced in R2014a

expand all