1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

secureCoding > 2の補数における事前条件の検査 > オーバーフローが発生しないことの確認

Last updated at Posted at 2015-05-08

引用: C/C++ セキュアコーディング 第2版 by Robert C. Seacordら

次のコードは加算オペランドについて事前条件の検査を行い、オーバーフローが発生しないことを確認している。

sample.c
signed int si1, si2, sum;

/* si1とsi2を初期化 */

unsigned int usum = (unsigned int)si1 + si2;

if ((usum ^ si1) & (usum ^ si2) & INT_MIN) {
    /* エラー処理 */
} else {
    sum = si1 + si2;
}

^はビットXOR
http://www.cppdrive.jp/cstart/ope/index6.html

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?