sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

New Rule Idea: Buffers rented from buffer pools should be returned

Open sebastien-marichal opened this issue 6 months ago • 1 comments

Buffers allocated using buffer pools such as ArrayPool should be returned after usage.

Noncompliant example

void Method(Stream stream)
{
    var buffer = ArrayPool<byte>.Shared.Rent(512); // Noncompliant buffer is not returned
    var len = stream.Read(buffer, 0, 512);
    // ...
}

Compliant example

void Method(Stream stream)
{
    var buffer = ArrayPool<byte>.Shared.Rent(512); // Compliant
    var len = stream.Read(buffer, 0, 512);
    // ...
    ArrayPool<byte>.Shared.Return(buffer);
}

Notes

MemorePool does not have a Return and needs more investigation.

sebastien-marichal avatar Aug 06 '24 12:08 sebastien-marichal