-
Notifications
You must be signed in to change notification settings - Fork 4
Gendarme.Rules.Interoperability.DoNotAssumeIntPtrSizeRule(2.10)
Assembly: Gendarme.Rules.Interoperability
Version: 2.10
This rule checks for code which casts an IntPtr or UIntPtr into a 32-bit (or smaller) value. It will also check if memory read with the Marshal.ReadInt32 and Marshal.ReadInt64 methods is being cast into an IntPtr or UIntPtr. IntPtr is generally used to reference a memory location and downcasting them to 32-bits will make the code fail on 64-bit CPUs.
Bad example (cast):
int ptr = dest.ToInt32 ();
for (int i = 0; i < 16; i++) {
Marshal.StructureToPtr (this, (IntPtr)ptr, false);
ptr += 4;
}
Bad example (Marshal.Read*):
// that won't work on 64 bits platforms
IntPtr p = (IntPtr) Marshal.ReadInt32 (p);
Good example (cast):
long ptr = dest.ToInt64 ();
for (int i = 0; i < 16; i++) {
Marshal.StructureToPtr (this, (IntPtr) ptr, false);
ptr += IntPtr.Size;
}
Good example (Marshal.Read*):
IntPtr p = (IntPtr) Marshal.ReadIntPtr (p);
- This rule is available since Gendarme 2.0 but was named DoNotCastIntPtrToInt32Rule before 2.2
Note that this page was autogenerated (3/17/2011 9:31:58 PM) based on the xmldoc
comments inside the rules source code and cannot be edited from this wiki.
Please report any documentation errors, typos or suggestions to the
Gendarme Mailing List. Thanks!