Explanation: Memory overflow, memory leak, memory out of bounds, buffer overflow, stack overflow

Source: Internet
Author: User
Tags sprintf
Memory overflow is the memory you want to allocate more than the system can give you, the system is not satisfied

Demand, resulting in an overflow.

A

================================================================ memory leak is when you use the system to allocate memory for use (new), but it is not returned when it is finished ( Delete), as a result, the memory you are applying for cannot be
accessed again (you may have lost its address), and the system cannot assign it again
to the program you need. A dish can only hold 4 fruits in every way, and you have 5
, and you can't eat it on the ground. This is the overflow. For example, stack, stack full time to do in the
stack must produce space overflow, called overflow, stack empty when the stack also produces space overflow, said
for the underflow. Is that the allocated memory is not enough to drop the sequence of data items, called a memory overflow. The
is categorized in the manner in which memory leaks can be grouped into 4 categories:
1. Frequent memory leaks. The code that has a memory leak is executed multiple times, causing a memory leak each time it is executed by the
.
2. Accidental memory leaks. The code that occurs with a memory leak occurs only in certain environments or in the process of
manipulation. The frequent and incidental are relative. For a particular environment, the occasional
may become a regular hair. So test environments and test methods are critical to detecting memory
leaks.
3. A one-time memory leak. The code that occurs with a memory leak is executed only once, or by a
defect in the algorithm, which will always have a single and one memory leak. For example, allocating memory in the
class's constructor does not release the memory in the destructor, so the inside
save leak occurs only once.
4. An implicit memory leak. The program keeps allocating memory while it is running, but it does not release memory until the
bundle is closed. Strictly speaking, there is no memory leak, because the final
program frees up all of the requested memory. However, for a server program, a few
days, weeks, or months are required to run, and the release of memory in a timely manner may lead to the eventual exhaustion of the system's
memory. So, we call this kind of memory leak as an implicit memory leak.
from the point of view of the user's use of the program, the memory leak itself does not do any harm, as
General users, do not feel the existence of a memory leak. What really harms is the memory drain
The accumulation of leaks, which ultimately consumes all the memory of the system. From this perspective, a
sex memory leak is harmless because it does not accumulate, and implicit memory leaks harm

The sex is very large, because it is more difficult to detect than the frequent and occasional memory

=================================================================

Memory out of Bounds:

What is memory access out of bounds, in short, you apply a piece of memory to the system, which is beyond the scope of your application when using this memory.

Memory out of bounds, such errors caused by a great deal of uncertainty, sometimes large, sometimes small, sometimes may not affect the operation of the program, it is this difficult to reproduce the error is the most fatal, once the error is devastating.


What causes memory to be used out of bounds. There are several situations that can be used for reference:
Example 1:
Char buf[32] = {0};
for (int i=0; i<n; i++)//n < or n > 32
{
Buf[i] = ' x ';
}
....
Example 2:
Char buf[32] = {0};
String str = "This is a test sting!!!!";
sprintf (BUF, "This is a test buf!string:%s", Str.c_str ()); Out of the buffer space
....
Example 3:
String str = "This is a test string!!!!";
Char buf[16] = {0};
strcpy (buf, Str.c_str ()); Out of the buffer space

Similar and hidden functions are: strcat,vsprintf, etc.
Similarly, some memory operation functions such as memcpy, memset, Memmove must be noted when used.

When such a code is run, errors are unavoidable, and the consequences are uncertain, which can often result in the following consequences:

1. Destroy the memory allocation information data in the heap, especially the memory information data of the dynamically allocated memory block, because the operating system needs to access the data when allocating and releasing the memory block, and if the data is corrupted, several of the following situations may occur.
GLIBC detected * * * FREE (): Invalid pointer:
GLIBC detected * * * malloc (): Memory Corruption:
GLIBC detected * * Double free or corruption (out): 0x00000000005c18a0 * * * *
GLIBC detected * * * corrupted double-linked list:0x00000000005ab150

2. The destruction of the program's own other objects of memory space, this damage will affect the implementation of the program is not correct, of course, will also induce coredump, such as the destruction of the pointer data.

3. Destroy the free memory block, luckily, this will not cause any problems, but who knows when misfortune will befall.

Often, it is accidental that code errors are fired, in other words, your program has been normal before, perhaps because you added two member variables for the class, or changed a part of the code, Coredump frequently, and your increased code will never have any problems, then you should consider whether some memory is corrupted.

The principle of troubleshooting, the first is to ensure that the error can be reproduced, according to the wrong estimation of the possible links, step-by-step reduction of code, reduce the scope of the investigation.
Check all memory operation functions to check for possible memory bounds. Common Memory operation functions:
sprintf snprintf
vsprintf vsnprintf
strcpy strncpy strcat
memcpy memmove memset bcopy

If it is useful to write a dynamic library of your own, make sure that the compilation of the dynamic library is consistent with the environment in which the program is compiled.

=================================================================

Buffer overflow:

A buffer overflow is a data overlay on legitimate data that exceeds the capacity overflow of the buffer itself when the computer fills the buffer with the number of bits of data. Ideally, a program checking data length does not allow you to enter characters that exceed the length of the buffer, but most programs assume that the data length always matches the allocated storage space. This is the hidden danger of buffer overflow. The buffer used by the operating system is also called "stack." Between each operation process, the instruction is temporarily stored in the stack, and a buffer overflow occurs on the stack.

Stack Overflow:

Stack overflow is a kind of buffer overflow. The fact that a useful memory cell is overwritten because of a buffer overflow can often cause unpredictable consequences. In the process of running, in order to temporarily access the data needs, generally have to allocate some memory space, usually called these spaces as buffers.   If you write data to a buffer that exceeds its length so that the buffer cannot be accommodated, a memory cell other than the buffer is overwritten, which is called a buffer overflow. Stack overflow is a kind of buffer overflow.

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.