Assert and program Security

Source: Internet
Author: User

Assertion, used for debugging errors. It is found during debugging and then tracked for errors!

What are the security requirements of this code when writing a program for others? Intuitively, we all know that the program should not crash. However, if a C/C ++ program exposes the header file containing the API, it cannot guarantee this. Often, if a C/C ++ program is provided to others using header files, its security guarantee should be "It will not crash when it is used correctly, the failure occurs as early as possible during incorrect use ".
When I first learned about assert, I confused the role and exception of assert. I thought assert was the same as the exception purpose. In fact, the C standard is quite clear, assert calls abort when the conditions are not met. Abort means that the manufacturing program crashes. That is to say, the use of assert is as early as possible to collapse during incorrect use ".
What does the error mean?
First of all, the "incorrect use" here can only be a programmer's error, rather than an incorrect operation by the end user, because the end user is always right, no matter how messy the end user is on your GUI, or enter some strange things in Cui, you cannot collapse, you can only tell users over and over again that you cannot do this.
Second, the interfaces provided by C ++ to advanced languages cannot crash. calls from advanced languages cannot be considered misuse regardless of the parameter. For example, if you use C ++ to write a COM control, it can be used to calculate the addition of integers. However, if a client program uploads a string, you cannot collapse, you should patiently tell the customer that the program should not upload strings. Otherwise, the browser will collapse when the user browses a webpage with the wrong JavaScript written.
Finally, when parsing the network protocol, you cannot collapse and never trust that the Protocol passed over from the network is correct, and you cannot rely on assert to ensure the correctness.

That is to say, the use of assert is limited to ensuring that the internal code of your own C/C ++ code is correctly written, and when you provide a library to others through a C/C ++ header file, the code is correctly written. In addition, when you use assert to check the customer's incorrect input, you must note in the document that such input is incorrect and the behavior is undefined. The best result of "undefined" behavior is collapse, and assert is used to ensure that this best result can appear, rather than simply continuing to work. You know, "undefined" behavior is logical even if the computer is exploding. Of course, we usually do not see the computer explosion, this is because the operating system ensures that the scope of harm to your undefined behaviors is limited to your processes. If you are not kidding, the probability of a computer explosion is very small, but it is quite common to let the code fly to the ground and you don't know why the error occurs.

# Data structures and algorithms

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.