Skip to content

Pass number of bytes to Buffer.BlockCopy #33765

Closed
dotnet/roslyn-analyzers
#5242

Description

@terrajobst

The count argument is in bytes, but it's easy to accidentally do something like Buffer.BlockCopy(src, 0, dst, 0, src.Length), and if src is an array of elements larger than bytes, this is a bug.

Category: Reliability

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Small
    • Fixer: Not Applicable
  5. removed
    untriaged
    on Mar 21, 2020
  6. jeffhandley commented on Mar 27, 2020

    @jeffhandley
    Member

    If the first argument is an array of a primitive other than byte or sbyte, and the last argument is .Length for that array, then we should match and present the warning.

    Anything other than a simple .Length would not match. Any array of a non-primitive would not match.

    We can consider an expansion of the .Length value as an argument, identifying places where division is not used, we could match--but we would not match when division is used.

  7. carlossanlop commented on Oct 30, 2020

    @carlossanlop
    Contributor

    Usage example:

    using System;
    
    namespace MyNamespace
    {
        class Sample
        {
            public static void Main()
            {
                int[] src = ...;
                int[] dst = ...;
    
                // Warn
                Buffer.BlockCopy(src, 0, dst, 0, src.Length);
    
                // Warn
                Buffer.BlockCopy(src, 0, dst, 0, dst.Length);
    
                // Warn
                int numBytes2 = src.Length;
                Buffer.BlockCopy(src, 0, dst, 0, numBytes2);
    
                // Warn
                int numBytes3 = dst.Length;
                Buffer.BlockCopy(src, 0, dst, 0, numBytes3);
    
                // Do not warn
                Buffer.BlockCopy(src, 0, dst, 0, 5);
    
                // Do not warn
                int numBytes = ...;
                Buffer.BlockCopy(src, 0, dst, 0, numBytes);
    
                // Do not warn
                Buffer.BlockCopy(src, 0, dst, 0, GetNumBytes());
    
    
               // Allowed array data types
                
                byte[] srcByte = ...;
                byte[] dstByte = ...;
    
                // Do not warn if underlying array type is byte
                Buffer.BlockCopy(srcByte, 0, dstByte, 0, srcByte.Length);
    
                // Do not warn if underlying array type is byte
                Buffer.BlockCopy(srcByte, 0, dstByte, 0, dstByte.Length);
    
                sbyte[] srcSbyte = ...;
                sbyte[] dstSbyte = ...;
                // Do not warn if underlying array type is sbyte
                Buffer.BlockCopy(srcSbyte, 0, dstSbyte, 0, srcSbyte.Length);
    
                // Do not warn if underlying array type is sbyte
                Buffer.BlockCopy(srcSbyte, 0, dstSbyte, 0, dstSbyte.Length);
            }
    
            static int GetNumBytes()
            {
                return ...;
            }
        }
    }
    • Suggested severity: warning.
    • No code fixer expected for this.

    Question

    @jeffhandley 's last suggestion to detect if a division is used would not warn anyway because the passed argument would not exactly be src.Length or dst.Length, but an expression. Would we be interested in detecting if the expression is src.Length/sizeof(int) or dst.Length/sizeof(int) or a division that would make sense to determine the size in bytes, depending on the data type of the array?

  8. added
    api-ready-for-review
    and removed
    api-suggestion
    on Oct 30, 2020
  9. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 2, 2021
  10. terrajobst commented on Feb 2, 2021

    @terrajobst
    ContributorAuthor

    Video

    • On by default as warning seems reasonable, given the scoped nature
    • Adding a fixer that offers to multiply the length or call Array.Copy instead
  11. carlossanlop commented on Feb 2, 2021

    @carlossanlop
    Contributor

    Thanks. The Buffer.BlockCopy documentation remarks should also be expanded to also describe the suggestions that this analyzer will provide: https://docs.microsoft.com/en-us/dotnet/api/system.buffer.blockcopy?view=net-5.0#remarks

  12. 2 remaining items

  13. modified the milestones: Future, 6.0.0 on Jun 14, 2021
  14. modified the milestones: 6.0.0, Future on Jul 12, 2021
  15. mahdiva commented on Jul 20, 2021

    @mahdiva

    Just bringing up a point for clarification. If we had the following code, I don't think the analyzer should be triggered

    using System;
    
    class Program
    {
        static void Main()
        {
            int[] src = new int[] {1, 2, 3, 4};
            int[] dst = new int[] {0, 0, 0, 0};
            int numOfBytes = src.Length;
            
            SomeFunction(src, dst, numOfBytes);
        }
    
        static void SomeFunction(int[] src, int[] dst, int count)
        {
            Buffer.BlockCopy(src, 0, dst, 0, count); // I don't think it's possbile to know where count is coming from. Looking just at this expression, `count`'s variable declaration will be the parameter in the function definition. I think we should NOT report a diagnostic in this case
        }
    }
  16. removed
    help wanted
    on Aug 6, 2021
  17. modified the milestones: Future, 6.0.0 on Aug 6, 2021
  18. jeffhandley commented on Aug 22, 2021

    @jeffhandley
    Member
  19. ghost locked as resolved and limited conversation to collaborators on Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Runtimecode-analyzer

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions