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
Our
Safe*Handle
classes usually expose apublic 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