JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.0p0 Hot Tips | CONFIDENTIAL | CodeSecure Inc |
キャストによる型変換が、値変更を引き起こします。
| クラス名 | Cast Alters Value | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 日本語クラス名 | キャストによる値変更 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| クラス分類 | セキュリティ (security) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ニーモニック | LANG.CAST.VALUE | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| カテゴリー |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 対応言語 | C および C++ で利用可能です。 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル
(configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Cast Alters Value" |
以下のような場合、型変換は値を変化させます。
In many cases, a warning of this class indicates an inadvertent value change that may cause problems later. Out-of-range casts to enumeration type in C++ are more serious, since the resulting behavior is undefined rather than defined but (possibly) unexpected. Cast Alters Value warnings in this latter category will therefore generally have higher Rank.
/* EXAMPLE 1: LANG.CAST.VALUE.c * - a simple case */ unsigned int lang_cast_value(void){ int x; unsigned int y; x = -10; y = (unsigned int)(x+5); /* 'Cast Alters Value' warning issued here */ return y; }
// EXAMPLE 2: LANG.CAST.VALUE.cpp // - casting to enumeration type in C++. // // Comments in the following code example use notation [x,y] to // represent the set of values from x to y, including both x and y. namespace lang_cast_value { // Value range for Color is [0,7] enum Color {red, // == 0 orange, yellow, green, blue, indigo, violet // == 6 }; int use_color(void){ Color a = static_cast<Color>(5); // ok: within range [0,7] Color b = static_cast<Color>(7); // ok: within range [0,7] Color c = static_cast<Color>(8); // 'Cast Alters Value' warning issued here Color d = static_cast<Color>(-1); // 'Cast Alters Value' warning issued here return a + b + c + d; } // Value range for Compass is [-4,3] enum Compass {north = -3, south, // == -2 east, // == -1 west // == 0 }; int use_compass(void){ Compass a = static_cast<Compass>(-5); // 'Cast Alters Value' warning issued here Compass b = static_cast<Compass>(-4); // ok: within range [-4,3] Compass c = static_cast<Compass>(-2); // ok: within range [-4,3] Compass d = static_cast<Compass>(3); // ok: within range [-4,3] Compass e = static_cast<Compass>(4); // 'Cast Alters Value' warning issued here return a + b + c + d + e; } }
The value range representable by an enumeration E is determined as specified in recent versions of the C++ language standard:
For example, suppose we have the following enumeration.
enum Walk {left, right, forward}; /* left==0, right==1, forward==2 */
A type is not specified for this enumeration, so its values are deemed to be those of the smallest bit field that can represent all enumerators specified for Walk: {0,1,2}. The values of Walk are therefore those in the interval between 0 and 3 (including both 0 and 3), which we can write as [0,3]. Note that there is no named enumerator with value 3, but it is still considered a value of Walk.
Some other example cases:
enum A{}; // Requires a 0-bit bit field. Can only represent the value 0.
enum B{BB}; // Requires a 0-bit bit field. Can only represent the value 0.
enum C{CC=-1}; // Requires a 1-bit signed bit field. Can represent -1..0.
enum D{DD=1}; // Requires a 1-bit unsigned bit field. Can represent 0..1.
enum E{EE=-2}; // Requires a 2-bit signed bit field. Can represent -2..1.
enum F{FF=-5}; // Requires a 4-bit signed bit field. Can represent -8..7.
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。