メインコンテンツ

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

MISRA C++:2008 Rule 5-0-12

Signed and unsigned char type shall be used only for the storage and use of numeric values.

説明

ルール定義

Signed and unsigned char type shall be used only for the storage and use of numeric values. 1

根拠

C/C++ には 3 種類の char があります。

  • プレーンな char

  • signed char

  • unsigned char

プレーンな char の符号属性は処理系定義です。プレーンな char は、他の型と互換的に使用することはできません。たとえば、char が符号なしであると想定し、unsigned char を使用して文字を格納するとします。また、自分が使用している実装では文字が符号付きとして解釈されるとします。このような状況では、コードが予期しない方法で動作し、診断が難しいバグにつながる可能性があります。

MISRA C++:2008 では、この 3 種類の char を異なる用途に使用するように制限を設けています。signed 型と unsigned char 型は、数値とストレージに適しています。プレーンな char は文字データに適しています。プレーンな char を使用する意図がある場合には、signed または unsigned char の使用を回避します。

このルールはこれらの char 型のさまざまな typedef (uint8_tint8_t など) にも適用されます。MISRA C++:2008 Rule 3-9-2 を参照してください。

Polyspace 実装

Polyspace® は、プレーンな charsigned char または unsigned char に暗黙的に変換されると、このルールの違反を報告します。

トラブルシューティング

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

すべて展開する

typedef signed   char         int8_t;
typedef unsigned char         uint8_t;

namespace foo
{
	int8_t        ch_1   =   'a';     // Noncompliant
	uint8_t       ch_2  =   '\r';     // Noncompliant
	char          ch_3  =   'A';      // Compliant
	int8_t        num_1   =   10;     // Compliant
	uint8_t       num_2   =   12U;    // Compliant
	signed char   num_3   =   11;     // Compliant 

};

この例では、Polyspace は signed char および unsigned char を使用した文字データの格納にフラグを設定します。文字リテラルの型はプレーンな char 型であり、これらのプレーンな char 型の、明示的な signed 型または unsigned char 型への暗黙的な変換に、Polyspace はフラグを設定します。

チェック情報

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

バージョン履歴

R2015a で導入


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.