Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA2022 is incompatible with .NET Framework 4.8 #7494

Open
N-Olbert opened this issue Dec 3, 2024 · 1 comment
Open

CA2022 is incompatible with .NET Framework 4.8 #7494

N-Olbert opened this issue Dec 3, 2024 · 1 comment

Comments

@N-Olbert
Copy link

N-Olbert commented Dec 3, 2024

Analyzer

Diagnostic ID: CA2022: Avoid inexact read with Stream.Read

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 9.0.0

Describe the bug

This rule raises an issue within .NET Framework 4.8, but the suggested method ReadExactly does not exist

Steps To Reproduce

  1. Create new .NET Framework 4.8 console app.
  2. Insert the following method anywhere:
        void M1(Stream stream, byte[] buffer)
        {
            // CA2022 violation.
            stream.Read(buffer, 0, buffer.Length);
        }

Expected behavior

No warning at all

Actual behavior

CA2022 violation reported

Additional context

#7208
#69159

@mpidash
Copy link
Contributor

mpidash commented Jan 6, 2025

You are correct that the suggested method ReadExactly does not exist in .NET Framework 4.8. But the doc page suggests another way to fix the violation:

To fix a violation, either check the return value (which is the total number of bytes read into the buffer) ...

When you check the return value (which is the recommended approach and the reason this rule exists, see here) the violation is no longer reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants