C/C++ unsigned integer overflow
i'm reading an article about integer security . here's the link: http://ptgmedia.pearsoncmg.com/images/0321335724/samplechapter/seacord_ch05.pdf
In page 166,there is said:
A computation involving unsigned operands can never overflow,because a result that cannot be represented by the resulting unsigned integer type is reduced modulo to the number that is one greater than the largest value that can be represented by the resulting type.
What does it mean? appreciate for reply.
It means the value "wraps around".
UINT_MAX + 1 == 0
UINT_MAX + 2 == 1
UINT_MAX + 3 == 2
.. and so on
As the link says, this is like the modulo operator: http://en.wikipedia.org/wiki/Modulo_operation
No overflow?
"Overflow" here means "producing a value that doesn't fit the operand". Because arithmetic modulo is applied, the value always fits the operand, therefore, no overflow.
In other words, before overflow can actually happen, C++ will already have truncated the value.
Modulo?
Taking a value modulo some other value means to apply a division, and taking the remainder.
For example:
0 % 3 = 0 (0 / 3 = 0, remainder 0)
1 % 3 = 1 (1 / 3 = 0, remainder 1)
2 % 3 = 2 (2 / 3 = 0, remainder 2)
3 % 3 = 0 (3 / 3 = 1, remainder 0)
4 % 3 = 1 (4 / 3 = 1, remainder 1)
5 % 3 = 2 (5 / 3 = 1, remainder 2)
6 % 3 = 0 (6 / 3 = 2, remainder 0)
...
이 모듈로는 서명되지 않은 전용 계산 결과에 적용되며, 이 모듈은 유형이 보유할 수 있는 최대값입니다.예를 들어, 최대값이 2^16=32768이면,32760 + 9 = (32760 + 9) % (32768+1) = 0
.
그 말은 당신이 그 신호를 바꿀 수 없다는 것을 의미합니다.unsigned
계산은 하지만 예상치 못한 결과를 초래할 수 있습니다.서명되지 않은 8비트 값이 있다고 가정해 보겠습니다.
uint8_t a = 42;
and we add 240 to that:
a += 240;
it will not fit, so you get 26.
Unsigned math is clearly defined in C and C++, where signed math is technically either undefined or implementation dependent or some other "things that you wouldn't expect may happen" wording (I don't know the exact wording, but the conclusion is that "you shouldn't rely on the behaviour of overflow in signed integer values")
보여줄 하나의 예시unsigned data type wraps around
오버플로 대신:
unsigned int i = std::numeric_limits<unsigned int>::max(); // (say) 4294967295
할당하기-ve
에 걸맞는unsigned
권장하지 않지만 예시적인 목적으로 아래에 사용합니다.
unsigned int j = -1; // 4294967295 wraps around(uses modulo operation)
unsigned int j = -2; // 4294967294
서명되지 않은 문서 시각화(0 to max)
의 모듈에 관하여 범위를 정합니다.max+1
(여기서 최대 = 2^n):
Range : 0, 1, 2,......., max-2, max-1, max
.................................................................................
Last-to-First : -(max+1), -max, -(max-1),......., -3, -2, -1
First-to-Last : max+1, max+2, max+3,......., max+max-1, max+max, max+max+1
Modulo Addition Rule: (A + B) % C = (A % C + B % C) % C
[max + max + 1] % (max + 1) = [(max) + (max + 1)] % (max + 1)
= [(max % (max + 1)) + ((max + 1) % (max + 1))] % (max + 1)
= [(max % (max + 1)) + 0] % (max + 1)
= [max] % (max + 1)
= max
언급URL : https://stackoverflow.com/questions/16056758/c-c-unsigned-integer-overflow
'programing' 카테고리의 다른 글
Angularjs에서 모든 쿠키를 제거하는 방법? (0) | 2023.10.15 |
---|---|
각 루프에 대한 자바스크립트 간의 차이.각 루프에 대해 in 및 angular.? (0) | 2023.10.15 |
요소의 중심 블럭 요소 (0) | 2023.10.15 |
봄 mvc에서 서블릿 3.1을 사용하는 방법? (0) | 2023.10.15 |
SQLLlchemy: 적어도 하나의 다대다 관련 테이블에서 멤버 자격으로 필터링 (0) | 2023.10.15 |