Freigeben über


CA2111: Pointers should not be visible

Item Value
RuleId CA2111
Category Microsoft.Security
Breaking change Breaking

Cause

A public or protected System.IntPtr or System.UIntPtr field is not read-only.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

IntPtr and UIntPtr are pointer types that are used to access unmanaged memory. If a pointer is not private, internal, or read-only, malicious code can change the value of the pointer, potentially allowing access to arbitrary locations in memory or causing application or system failures.

If you intend to secure access to the type that contains the pointer field, see CA2112: Secured types should not expose fields.

How to fix violations

Secure the pointer by making it read-only, internal, or private.

When to suppress warnings

Suppress a warning from this rule if you do not rely on the value of the pointer.

Example

The following code shows pointers that violate and satisfy the rule. Notice that the non-private pointers also violate the rule CA1051: Do not declare visible instance fields.

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;
   }
}

CA2112: Secured types should not expose fields

CA1051: Do not declare visible instance fields

See also