gpt4 book ai didi

c - 有符号整数的未定义行为和 Apple 安全编码指南

转载 作者:行者123 更新时间:2023-12-04 09:36:28 25 4
gpt4 key购买 nike

Apple Secure Coding Guide说如下(第 27 页):

Also, any bits that overflow past the length of an integer variable (whether signed or unsigned) are dropped.

然而,关于有符号整数溢出C standard (89)说:

An example of undefined behavior is the behavior on integer overflow.

If an exception occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not representable), the behavior is undefined.

编码指南错了吗?这里有什么我不明白的吗?我自己不相信 Apple Secure 编码指南会出错。

最佳答案

这是第二个意见,来自静态分析器described作为检测未定义的行为:

int x;

int main(){
x = 0x7fffffff + 1;
}

分析器是这样运行的:

$ frama-c -val -machdep x86_32 t.c

它产生:

[kernel] preprocessing with "gcc -C -E -I.  t.c"
[value] Analyzing a complete application starting at main
...
t.c:4:[kernel] warning: signed overflow. assert 0x7fffffff+1 ≤ 2147483647;
...
[value] Values at end of function main:
NON TERMINATING FUNCTION

这意味着程序 t.c 包含未定义的行为,并且它的执行不会在不导致未定义行为的情况下终止。

关于c - 有符号整数的未定义行为和 Apple 安全编码指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com