Solution to the cross-border problem of C Arithmetic Operators

Source: Internet
Author: User

A large number of security vulnerabilities are caused by the subtle details of Computer Arithmetic Operations. The specific C language, such as conversion between the number of symbols and the number of unsigned numbers, unexpected errors and security vulnerabilities may occur when arithmetic operations are out of bounds. There are countless specific cases.

As a system programmer, it is necessary to have a deep understanding of these details. This article will refer to the csapp to introduce how to determine the cross-border problems of arithmetic operations.

(Although the code in this article has undergone a lot of tests, I still cannot ensure the correctness of the code. I hope you can correct it ).
The principle is "pendulum theorem, no proof, write code ". the detailed proof process is explained in detail in csapp, and it is not too difficult. the key theorem is used to write code. go ~
Problem 1: The addition of the unsigned number is out of bounds.
[Theorem]


[Understanding]
This theorem is relatively easy and acceptable.

Copy codeThe Code is as follows:/* Determine whether arguments can be added without overflow */
Int uadd_ OK (unsigned int x, unsigned int y)
{
Return! (X + y <x );
}

Problem 2: the subtraction of unsigned numbers out of the border
[Theorem]

[Understanding]
1. there is no subtraction in the computer, x-y = x + (-y). Here-y is the addition inverse element of the preceding y. both signed and unsigned are converted to addition operations. only the definition of the inverse element of addition is different.

3. C language assurance-x = ~ X + 1; this method is equivalent to the above formula.
4. s = x-y = x + (-y). No overflow is equivalent to y not 0 or! Uadd_ OK (x,-y ).

Copy codeThe Code is as follows:/* Determine whether argumcnt can be substracted without overflow */
Int usub_ OK (unsigned int x, unsigned int y)
{
Return! Y |! Uadd_ OK (x,-y );
}

Question 3: Multiplication of unsigned numbers out of bounds
[Theorem]


[Understanding]
Equivalent Conditions can be deduced from each other.

Copy codeThe Code is as follows:/* Determine whether arguments can be multiplied without overflow */
Int umul_ OK (unsigned int x, unsigned int y)
{
Unsigned int p = x * y;
Return! X | p/x = y;
}

Question 4: The addition of signed numbers out of bounds
[Theorem]
The equivalent conditions for two signed numbers x and y. Out-of-bounds are x, y is negative, x + y is positive or x, y is positive, and x + y is negative.
[Understanding]
This theorem is relatively easy.Copy codeThe Code is as follows:/* Determine whether arguments can be added without overflow */
Int tadd_ OK (int x, int y)
{
Return! (X <0 & y <0 & x + y> 0 | x> 0 & y> 0 & x + y <0 );
}

Question 5: out-of-bounds subtraction of signed numbers
[Theorem]


[Understanding]
Just like the unsigned subtraction, the definition of addition inverse elements is different, but the bit mode is the same. C can ensure-x = ~ X + 1. It can also be discussed in two cases. See the code.

Copy codeThe Code is as follows:/* Determine whether arguments can be subtracted without overflow */
Int tsub_ OK (int x, int y)
{
# If 0
If (y = INT_MIN)
Return x <0;
Else
Return tadd_ OK (x,-y );
# Endif
Return y = INT_MIN & x <0 | y! = INT_MIN & tadd_ OK (x,-y );
}

Question 6: Multiplication of signed numbers out of the border
[Theorem]
It is exactly the same as the non-Signed multiplication.Copy codeThe Code is as follows:/* Determine whether arguments can be multiplied without overflow .*/
Int tmul_ OK (int x, int y)
{
# If 0
Int p = x * y;
Return! X | p/x = y;
# Endif
Return umul_ OK (x, y);/* call directly */
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.