Export (0) Print
Expand All
Expand Minimize

CA2111: Pointers should not be visible

TypeName

PointersShouldNotBeVisible

CheckId

CA2111

Category

Microsoft.Security

Breaking Change

Breaking

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

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.

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

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

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;
   }
}
Show:
© 2014 Microsoft