You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
usingSystem;namespaceMyNamespace{classSample{publicstaticvoidMain(){int[]src=...;int[]dst=...;// WarnBuffer.BlockCopy(src,0,dst,0,src.Length);// WarnBuffer.BlockCopy(src,0,dst,0,dst.Length);// WarnintnumBytes2=src.Length;Buffer.BlockCopy(src,0,dst,0,numBytes2);// WarnintnumBytes3=dst.Length;Buffer.BlockCopy(src,0,dst,0,numBytes3);// Do not warnBuffer.BlockCopy(src,0,dst,0,5);// Do not warnintnumBytes=...;Buffer.BlockCopy(src,0,dst,0,numBytes);// Do not warnBuffer.BlockCopy(src,0,dst,0,GetNumBytes());// Allowed array data typesbyte[]srcByte=...;byte[]dstByte=...;// Do not warn if underlying array type is byteBuffer.BlockCopy(srcByte,0,dstByte,0,srcByte.Length);// Do not warn if underlying array type is byteBuffer.BlockCopy(srcByte,0,dstByte,0,dstByte.Length);sbyte[]srcSbyte=...;sbyte[]dstSbyte=...;// Do not warn if underlying array type is sbyteBuffer.BlockCopy(srcSbyte,0,dstSbyte,0,srcSbyte.Length);// Do not warn if underlying array type is sbyteBuffer.BlockCopy(srcSbyte,0,dstSbyte,0,dstSbyte.Length);}staticintGetNumBytes(){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?
Just bringing up a point for clarification. If we had the following code, I don't think the analyzer should be triggered
usingSystem;classProgram{staticvoidMain(){int[]src=newint[]{1,2,3,4};int[]dst=newint[]{0,0,0,0};intnumOfBytes=src.Length;SomeFunction(src,dst,numOfBytes);}staticvoidSomeFunction(int[]src,int[]dst,intcount){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}}
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
jeffhandley commentedon Mar 27, 2020
If the first argument is an array of a primitive other than
byteorsbyte, and the last argument is.Lengthfor that array, then we should match and present the warning.Anything other than a simple
.Lengthwould not match. Any array of a non-primitive would not match.We can consider an expansion of the
.Lengthvalue as an argument, identifying places where division is not used, we could match--but we would not match when division is used.carlossanlop commentedon Oct 30, 2020
Usage example:
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.Lengthordst.Length, but an expression. Would we be interested in detecting if the expression issrc.Length/sizeof(int)ordst.Length/sizeof(int)or a division that would make sense to determine the size in bytes, depending on the data type of the array?terrajobst commentedon Feb 2, 2021
Video
Array.Copyinsteadcarlossanlop commentedon Feb 2, 2021
Thanks. The
Buffer.BlockCopydocumentation 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#remarks2 remaining items
mahdiva commentedon Jul 20, 2021
Just bringing up a point for clarification. If we had the following code, I don't think the analyzer should be triggered
jeffhandley commentedon Aug 22, 2021
Closed via dotnet/roslyn-analyzers#5242