为什么 FxCop 会警告此 C# 代码中的溢出 (CA2233)?

发布于 2024-08-28 13:19:56 字数 370 浏览 0 评论 0原文

我有以下函数从高字节和低字节获取 int:

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

当我使用 FxCop 分析程序集时,我收到以下严重警告:

CA2233:操作不应溢出
算术运算不应该 没有首先验证就完成了 防止溢出的操作数。

我不明白这怎么可能溢出,所以我只是假设 FxCop 过于热心。
我错过了什么吗?可以采取哪些步骤来纠正我所遇到的问题(或者至少让 FxCop 警告消失!)?

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!)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫弦 2024-09-04 13:19:56

它以字节计算的方式进行它们。

试试这个

return (int)high * ((int)byte.MaxValue + 1) + (int)low;

It is doing them as byte calculations.

Try this

return (int)high * ((int)byte.MaxValue + 1) + (int)low;
黄昏下泛黄的笔记 2024-09-04 13:19:56

字节加法和乘法结果都是整数。这里的最大值是 65535,不会溢出 int。只是压制错误。

byte a = 1;
byte b = 2;
object obj = a + b

obj 的类型为 int

试试这个:

        byte high = 255;
        byte low = 255;
        checked
        {
            int b = high * (byte.MaxValue + 1) + low;   
        }

没问题。

或者试试这个

Byte addition and mult results are ints. The max value here is 65535 which won't overflow an int. Just surpress the error.

byte a = 1;
byte b = 2;
object obj = a + b

obj has type int

Try this:

        byte high = 255;
        byte low = 255;
        checked
        {
            int b = high * (byte.MaxValue + 1) + low;   
        }

No problem.

or try this

吹泡泡o 2024-09-04 13:19:56

正如 丹尼尔·怀特 指出,您收到消息是因为“(byte.MaxValue + 1)”溢出一个字节。

但我不会进行转换和乘法,而是简单地按照下面的代码中的方式移动位:

public static int FromBytes(byte high, byte low) {
    return high << 8 | low;
}

作为副作用,此代码可能会执行得更好。我还没有检查生成的 IL 或 x86 以查看编译器和/或 JITter 是否足够智能来优化原始表达式。

As Daniel A. White pointed out, you get the message because "(byte.MaxValue + 1)" overflows a byte.

But instead of casting and multiplying, I would simply shift the bits as done in the code below:

public static int FromBytes(byte high, byte low) {
    return high << 8 | low;
}

As a side effect, this code will probably perform better. I haven't checked the resulting IL or x86 to see if the compiler and/or the JITter are smart enough to optimize the original expression.

把梦留给海 2024-09-04 13:19:56

以下是它最终停止对我抱怨 CA2233 的两种方法:

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

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

我认为这可能是规则中的一个错误。

Here's 2 ways that it finally stopped whining about CA2233 for me:

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

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

I think it might be a bug in the rule.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文