gpt4 book ai didi

c - 安全漏洞: What is the error in this piece of code?

转载 作者:行者123 更新时间:2023-11-30 17:16:05 27 4
gpt4 key购买 nike

我正在阅读this book我自己,只是为了好玩,并遇到了以下问题:

This code has a security vulnerability ; Can you find and fix it? :

  bool isValidAddition(unsigned short x, unsigned short y) 
{
if(x + y < x)
return false;
else
return true;
}

有人可以帮助我识别该漏洞吗?

最佳答案

我们知道,根据 C 标准,以下几点是正确的:

  • sizeof(short) <= sizeof(int) <= sizeof(long)
  • sizeof(short) >= 2 bytes , sizeof(int) >= 2 bytes, sizeof(long) >= 4 bytes
  • There is an implicit integer promotion of operand data types used in arithmetic expressions which is done by the compiler

因此,在上面的代码片段中执行以下操作:

改变

if(x + y < x) 

if((unsigned short)(x + y) < x) 

如果 int 为 4(或 >2)字节,则此方法有效

希望这有帮助:)

关于c - 安全漏洞: What is the error in this piece of code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787671/

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