Why is this an issue?

The SafeHandle.DangerousGetHandle method poses significant risks and should be used carefully. This method carries the inherent danger of potentially returning an invalid handle, which can result in resource leaks and security vulnerabilities. Although it is technically possible to utilize this method without encountering issues, doing so correctly requires a high level of expertise. Therefore, it is recommended to avoid using this method altogether.

What is the potential impact?

The SafeHandle.DangerousGetHandle method is potentially prone to leaks and vulnerabilities due to its nature and usage. Here are a few reasons why:

static void Main(string[] args)
{
    System.Reflection.FieldInfo fieldInfo = ...;
    SafeHandle handle = (SafeHandle)fieldInfo.GetValue(rKey);
    IntPtr dangerousHandle = handle.DangerousGetHandle(); // Noncompliant
}

Resources

Documentation

Articles & blog posts