メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MISRA C++:2008 Rule 6-6-1

Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement

説明

ルール定義

Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement 1

根拠

goto ステートメントを使用して、入れ子にされたブロックにジャンプすると複雑な制御フローになり、開発者の混乱を招いたり、予期しない結果が生じたりする可能性があります。予期しない結果を回避するには、goto ステートメントが参照するラベルを同じブロック内または goto ステートメントを含むブロック内に配置します。

Polyspace 実装

Polyspace® は、goto の移動先が goto ステートメントとは異なるブロック内にある場合にこの欠陥を報告します。goto の移動先が goto を含むブロック内にある場合、この欠陥は報告されません。

トラブルシューティング

ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

#include <iostream>

int x, y = 0;

void foo1()
{
    int i = 0;
    if (x <= 10) {
        goto err;                    //Noncompliant
    }

    if(x > 10) {
    err:
        std::cout << "Error encountered in loop" << std::endl;
    }

}

void foo2()
{
    for (x = 0; x < 100; ++x) {
        for (y = 0; y < 100; ++y) {
            if (x > y) {
                goto stop;            //Compliant
            }
            //...
        }
    }
stop:
    std::cout << "Error encountered in loop" << std::endl;
}

ラベル errgoto err とは別のコード ブロック内にあり、そのコード ブロック内には goto err のあるコード ブロックが含まれていないため、Polyspace は非準拠として goto ステートメントにフラグを設定します。

ラベル stopgoto stop と同じブロック内にありませんが、goto stop ステートメントを含むブロック内にあります。この動作は、準拠している動作です。

チェック情報

グループ: Statements
カテゴリ: 必要

バージョン履歴

R2013b で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.