
MKnafve (Community Member) asked a question.
I am scanning my application using Veracode, and it reports the following error:
The program fails to release or incorrectly releases the variable stream, which was previously allocated by a call to mscorlib_dll.System.IO.MemoryStream.!newinit_0_0().
The code where the issue is identified looks as follows:
using (var stream = new MemoryStream())
{
Serialize(stream, dataToStore);
var buffer = stream.GetBuffer();
blob.UploadFromByteArray(buffer, 0, buffer.Length);
}
I don't understand where the issue is. The Serialize method serializes an object into the memory stream and the call blob.Upload... uploads the data to Azure Blob Storage.
Since I'm wrapping the creation of the memory stream in a using-statement, I'm confident that the resource is released. Anyone have some idea what could cause the above flaw being reported for this code?
.png)
Hi @MKnafve (Community Member),
Thanks for your question. As you are correctly releasing the the stream variable via the using statement it is reasonable to propose a Mitigation By Design for this flaw if this code is within an asynchronous method. The issue with asynchronous methods is that objects may be disposed of in different threads than they were created, which is likely why the flaw was raised. However the use of using is the correct means by which to safely dispose of these resources. If the code is not in an asynchronous block then you may wish to instead propose a False Positive mitigation. You can learn how to propose mitigations if you're not familiar with the process here: https://help.veracode.com/r/improve_mitigation?section=mitigate__propose.
A colleague has answered a similar question here: https://community.veracode.com/s/question/0D52T00004lokokSAA/improper-resource-shutdown-or-release-net-core-22.
I hope that answers your question.
Thanks,
Anthony Fielding
Thanks @Anthony Fielding (Veracode) ,
I have two follow up questions:
Thanks!
Hello @MKnafve (Community Member),
Whilst the using statement appears correct and I recommend you propose a mitigation, we would like to look into this further. Please could you open a support case for this. Here's how you can log a case:
Many thanks,
Anthony Fielding