更新 : 2007 年 11 月
TypeName |
PointersShouldNotBeVisible |
CheckId |
CA2111 |
カテゴリ |
Microsoft.Security |
互換性に影響する変更点 |
あり |
原因
パブリックまたはプロテクトの System.IntPtr フィールドまたは System.UIntPtr フィールドが読み取り専用ではありません。
規則の説明
IntPtr と UIntPtr は、アンマネージ メモリにアクセスするときに使用するポインタの型です。ポインタがプライベート、内部、または読み取り専用のいずれでもない場合、悪意のあるコードで、ポインタの値が変更される可能性があります。結果的に、メモリの任意の位置にアクセスされたり、アプリケーション エラーやシステム エラーの原因になります。
ポインタのフィールドを含む型に対するアクセスを保護するには、「安全な型でフィールドを公開しないでください」を参照してください。
違反の修正方法
読み取り専用、内部、またはプライベートにすることで、ポインタを保護します。
警告を抑制する状況
ポインタの値に依存していない場合は、この規則による警告を抑制します。
使用例
この規則に違反するポインタと適合するポインタを次のコードに示します。ポインタをプライベートにしないと、規則「参照できるインスタンス フィールドを宣言しないでください」にも違反するので注意してください。
using System;
namespace SecurityRulesLibrary
{
public class ExposedPointers
{
// Violates rule: PointersShouldNotBeVisible.
public IntPtr publicPointer1;
public UIntPtr publicPointer2;
protected IntPtr protectedPointer;
// Satisfies the rule.
internal UIntPtr internalPointer;
private UIntPtr privatePointer;
public readonly UIntPtr publicReadOnlyPointer;
protected readonly IntPtr protectedReadOnlyPointer;
}
}