C#idiomatic C#
ca5367fundamentals/code-analysis/quality-rules

Code analysis

CA5367: Do not serialize types with pointer fields

Provides information about code analysis rule CA5367, including causes, how to fix violations, and when to suppress it.

CA5367 Security enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

Pointers are not type safe, which means you cannot guarantee the correctness of the memory they point at. So, serializing types with pointer fields is a security risk, as it may allow an attacker to control the pointer.

Rule description

This rule checks whether there’s a serializable class with a pointer field or property. Members that can’t be serialized can be a pointer, such as static members or fields marked with System.NonSerializedAttribute.

How to fix violations

Don't use pointer types for members in a serializable class or don't serialize the members that are pointers.

When to suppress warnings

Don't take the risk to use pointers in serializable types.

Pseudo-code examples

Violation

csharp
using System;

[Serializable()]
unsafe class TestClassA
{
    private int* pointer;
}

Solution 1

csharp
using System;

[Serializable()]
unsafe class TestClassA
{
    private int i;
}

Solution 2

csharp
using System;

[Serializable()]
unsafe class TestClassA
{
    private static int* pointer;
}