dotnet / pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
MIT License
2.12k stars 222 forks source link

Are our Safe*Handle.Null properties safe? #410

Closed AArnott closed 1 year ago

AArnott commented 5 years ago

Our Safe*Handle classes usually expose a public static readonly Null field, which is initialized to a null handle.

Is this safe? What if someone sets a struct's SafeHandle field to one of these instances and pass the struct into a p/invoke function call that initializes that handle? Would it not set the "null" instance to a non-null value? Further, if the same instance is used multiple times in a single p/invoke call, wouldn't that cause all of the parameters/fields to be set to the same value since they're sharing the same instance?

I realized this potential problem while authoring a fix for #408