Applet output-array out-of-bounds access to the hidden anti-Bot

Source: Internet
Author: User

Please refer to the following smallProgram--- A lot of attacks

# Include <stdio. h> # define Max 255int main () {unsigned char a [Max], I; for (I = 0; I <= max; I ++) A [I] = I; for (I = 0; I <= max; I ++) printf ("% d", a [I]); printf ("\ n"); Return 0 ;}

It seems that there is no problem. Let's take a closer look and find that the array is out of bounds. But let's take a closer look at it...

When I = 255, You can execute a [255] = 255. Because the array does not check whether the access to the array element is out of bounds, and then I ++, how much does I become? Note that this is unsigned. I + 1 will become 11111111 + 1 = 100000000. The highest bit is removed, then I = 0; then it becomes an endless loop. So there is no problem in compiling, but there is a problem in running.

Let's look at a similar example.

# Include <stdio. h> int main () {char K = 0; while (k> = 0) K ++; printf ("% d \ n", k); Return 0 ;}

The answer is-128, which is similar to the one above. After K is converted to the ASCII code value, K ++ runs until 11111111, and then adds 1, computer computing addition is the complement of computing, so the 100000000 high is removed, it is 00000000, then the complement, 11111111, and then this is-128. different from the one above, this is not unsigned, so there is a negative number.

I found that programming is never proficient without knowing the underlying operating mechanism.

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.