Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is FxCop warning about an overflow (CA2233) in this C# code?

I have the following function to get an int from a high-byte and a low-byte:

public static int FromBytes(byte high, byte low)
{
    return high * (byte.MaxValue + 1) + low;
}

When I analyze the assembly with FxCop, I get the following critical warning:

CA2233: OperationsShouldNotOverflow
Arithmetic operations should not be done without first validating the operands to prevent overflow.

I can't see how this could possibly overflow, so I am just assuming FxCop is being overzealous.
Am I missing something? And what steps could be taken to correct what I have (or at least make the FxCop warning go away!)?

like image 240
Matthew King Avatar asked Apr 15 '10 01:04

Matthew King


1 Answers

It is doing them as byte calculations.

Try this

return (int)high * ((int)byte.MaxValue + 1) + (int)low;
like image 133
Daniel A. White Avatar answered Oct 27 '22 11:10

Daniel A. White