亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 整數(shù)溢出跟整數(shù)比較的關(guān)系 [打印本頁]

作者: nswcfd    時(shí)間: 2015-09-17 18:15
標(biāo)題: 整數(shù)溢出跟整數(shù)比較的關(guān)系
本帖最后由 nswcfd 于 2015-09-17 18:16 編輯

前幾天在版面上問了-1>sizeof(int)之間的關(guān)系 http://www.72891.cn/forum.php?mod=viewthread&tid=4187986

之所以提出這個(gè)問題,是由于看到了一篇介紹integer overflow的帖子 http://www.phrack.org/issues/60/10.html#article

里面談到了一個(gè)bug模式
Here is classic example of a signedness bug:

    int copy_something(char *buf, int len){
        char kbuf[800];

        if(len > sizeof(kbuf)){         /* [1] */
            return -1;
        }

        return memcpy(kbuf, buf, len);  /* [2] */
    }

The problem here is that memcpy takes an unsigned int as the len parameter,
but the bounds check performed before the memcpy is done using signed
integers.  By passing a negative value for len, it is possible to pass the
check at [1], but then in the call to memcpy at [2], len will be interpeted
as a huge unsigned value, causing memory to be overwritten well past the
end of the buffer kbuf


個(gè)人感覺這個(gè)論述有點(diǎn)問題,如果傳入的len是負(fù)值,那么【1】的判斷應(yīng)該是成立的,
因?yàn)閟izeof是無符號值,負(fù)值在比較的時(shí)候(最高bit是1),肯定大于最大的正整數(shù)(最高bit是0),進(jìn)而大于sizeof(buf)了。

當(dāng)然,如果在某些平臺(tái)下,如果sizeof不是unsigned,或者負(fù)數(shù)小于無符號數(shù)是成立的,這樣bug就成立了。
不過問題又來了,什么樣的平臺(tái)下此bug成立呢?


作者: nswcfd    時(shí)間: 2015-09-24 16:57
鏈接是2012年的文章,也許那時(shí)候某款編譯器會(huì)認(rèn)為sizeof是int類型的?

作者: shang2010    時(shí)間: 2015-09-25 19:49
要擁抱變化




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2