Replies: 2 comments 1 reply
-
An Now, the contents of the object may be in some indeterminate state, but generally that only applies to the unmanaged resources that necessitated the
If the checks are referencing |
Beta Was this translation helpful? Give feedback.
-
My default mental model for a disposable object is as follows: If the object has been disposed, then the only information about this object, for users of this object to know, is that the object has been disposed; Whether this is a good model is subject to discussion. Assuming this model is the right one, let's check it with commit public void SignData(ReadOnlySpan<byte> data, Span<byte> destination, ReadOnlySpan<byte> context = default)
{
int signatureSizeInBytes = Algorithm.SignatureSizeInBytes;
if (destination.Length != signatureSizeInBytes)
{
throw new ArgumentException(
SR.Format(SR.Argument_DestinationImprecise, signatureSizeInBytes),
nameof(destination));
}
if (context.Length > MaxContextLength)
{
throw new ArgumentOutOfRangeException(
nameof(context),
context.Length,
SR.Argument_SignatureContextTooLong255);
}
ThrowIfDisposed();
SignDataCore(data, context, destination);
} Suppose an To avoid revealing such information, one can call Of course, this particular, simple example doesn't appear harmful, because the cryptographic algorithm being used is public information. However, at the vastness of .NET libraries, it's not easy to verify that fields accessed prior to various scattered The above is just my model, and since what I observed in the official .NET libraries are somewhat different, it indicates
Hence the dual nature of question and suggestion. For clarification, to respond to the previous top-level comment:
The object should be in a determinate state, the cannot-be-used state.
Just because something can be done does not mean it's a good idea to do it. Suppose an object
I agree that
Not applicable to the intended meaning of my original post, but I do thank you for pointing out a natural language usage issue --- my original post:
should be changed to:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
News has it that PQC is being integrated with .NET 10. I was excited and came here to read the code. Here's some curious finding/question/suggestion.
In many cases the check for disposed object (
ThrowIfDisposed();
) is done after all other argument verifications. This is the case for all methods inSystem/Security/Cryptography/SlhDsa.cs
. I also foundSystem/Security/Cryptography/CompositeMLDsa.cs
, whereExportCompositeMLDsaPublicKey
checks for disposed object before arguments. InSlhDsa.VerifyData
, object disposal is checked between two argument checks. It appears that verification for arguments themselves is done roughly in the order of parameter position.Is there official guidance on the order of checking for disposed object and verifying arguments? I'm eager to learn more about it. Yet without understanding the reasoning, for high-stake libraries such as crypto, checking for disposed object after verifying other arguments, especially if the verification of those arguments depends on the ex-object (state before disposal), keeps me up at night. (A perfunctory search suggests that argument verification might depend on the algorithm specifications, but not non-public state.)
If there isn't a known guidance, I would argue for checking object disposal as the first thing done in public instance methods. This ensures no state-dependent logic is executed if object has been disposed, and I think that's generally good hygiene. In addition, the receiver (instance) is the implicit first argument, so if argument verification is done roughly in the order of parameter position, then the validity of
this
should be the first thing to check.Beta Was this translation helpful? Give feedback.
All reactions