Strcpy, memcpy, memmove and memory overlap Analysis

Source: Internet
Author: User

I. Usage and implementation of strcpy functions:
[Cpp]
/*
 
Implementation in GNU-C (excerpt ):
 
*/
 
Char * strcpy (char * d, const char * s)
 
{
 
Char * r = d;
 
While (* d ++ = * s ++ ));
 
Return r;
 
}

/*

Implementation in GNU-C (excerpt ):

*/

Char * strcpy (char * d, const char * s)

{

Char * r = d;

While (* d ++ = * s ++ ));

Return r;

}
Have you found that this implementation is not very good .. At least it is not determined whether it is NULL. A better version:

[Cpp]
Char * strcpy (char * strDest, const char * strSrc)
{
Assert (strDest! = NULL) & (strSrc! = NULL ));
Char * address = strDest;
While (* strDest ++ = * strSrc ++ )! = '\ 0 ')

Return address;
}

Char * strcpy (char * strDest, const char * strSrc)
{
Assert (strDest! = NULL) & (strSrc! = NULL ));
Char * address = strDest;
While (* strDest ++ = * strSrc ++ )! = '\ 0 ')

Return address;
}
A good study of this Code is actually quite well written .. (Of course I did not write it )..

 

Ii. Usage and Implementation of the memcpy function:
Copy n Bytes from the starting position of the memory address in the source src to the starting position of the memory address in the target dest.

Microsoft implementation:

[Cpp]
Void * _ cdecl memcpy (void * dst, const void * src, size_t count)
{
Void * ret = dst;
# If defined (_ M_MRX000) | defined (_ M_ALPHA) | defined (_ M_PPC)
{
Extern void RtlMoveMemory (void *, const void *, size_t count );
RtlMoveMemory (dst, src, count );
}
# Else/* defined (_ M_MRX000) | defined (_ M_ALPHA) | defined (_ M_PPC )*/
/** Copy from lower addresses to higher addresses */
While (count --){
* (Char *) dst = * (char *) src;
Dst = (char *) dst + 1;
Src = (char *) src + 1;
}
# Endif/* defined (_ M_MRX000) | defined (_ M_ALPHA) | defined (_ M_PPC )*/
Return (ret );
}

Void * _ cdecl memcpy (void * dst, const void * src, size_t count)
{
Void * ret = dst;
# If defined (_ M_MRX000) | defined (_ M_ALPHA) | defined (_ M_PPC)
{
Extern void RtlMoveMemory (void *, const void *, size_t count );
RtlMoveMemory (dst, src, count );
}
# Else/* defined (_ M_MRX000) | defined (_ M_ALPHA) | defined (_ M_PPC )*/
/** Copy from lower addresses to higher addresses */
While (count --){
* (Char *) dst = * (char *) src;
Dst = (char *) dst + 1;
Src = (char *) src + 1;
}
# Endif/* defined (_ M_MRX000) | defined (_ M_ALPHA) | defined (_ M_PPC )*/
Return (ret );
}

Is it good .. It seems like everything is seamless !.. But did you notice a problem: Memory overlaps ..

What is memory overlap? That is, the two memory areas have a common part .. Is it SB's explanation .. If this problem exists, will it be copied as we expected? Note: understanding this is the key ..

Example: (reference an online example)

[Cpp]
Int a [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Memcpy (a + 4, a, sizeof (int) * 6)

Int a [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Memcpy (a + 4, a, sizeof (int) * 6) if it is implemented by Microsoft, the output should be 0123012301 (if you understand this, forget to see it ..)

The purpose we want to achieve is to output 0123012345 .. If you try this experiment, the output is indeed 0123012345 .. (I cannot explain this problem ). To avoid this situation, we have found a better function: memmove ..

 

Iii. Usage and implementation of the memmove Function
The usage is the same as that of the memcpy function ..

Implementation:

[Cpp]
Void * memmove (void * dest, const void * source, size_t count)
{
Assert (NULL! = Dest) & (NULL! = Source ));
Char * tmp_source, * tmp_dest;
Tmp_source = (char *) source;
Tmp_dest = (char *) dest;
If (dest + count <source) | (source + count) <dest ))
{// If no overlapping areas exist
While (count --)
* Tmp_dest ++ = * tmp_source ++;
}
Else
{// If there is overlap (reverse copy)
Tmp_source + = count-1;
Tmp_dest + = count-1;
While (count --)
* -- Tmp_dest = * -- tmp;
}
Return dest;
}

Void * memmove (void * dest, const void * source, size_t count)
{
Assert (NULL! = Dest) & (NULL! = Source ));
Char * tmp_source, * tmp_dest;
Tmp_source = (char *) source;
Tmp_dest = (char *) dest;
If (dest + count <source) | (source + count) <dest ))
{// If no overlapping areas exist
While (count --)
* Tmp_dest ++ = * tmp_source ++;
}
Else
{// If there is overlap (reverse copy)
Tmp_source + = count-1;
Tmp_dest + = count-1;
While (count --)
* -- Tmp_dest = * -- tmp;
}
Return dest;
}
It was amazing when I first saw this code. Later I found that there was a major Vulnerability (not to mention the minor issues), that is, there were two kinds of memory overlaps. In fact, I think the author has also come up with a solution ., It is probably careless.
: Src <dest & src + count> dest; dest <src & dest + count> src .. But the author on the Internet did not write it clearly. Next I will rewrite the above else section:

[Cpp]
Else
{
// Overlap 1: (reverse copy)
If (tmp_source <= tmp_dest & tmp_source + count> = tmp_dest)
{
Tmp_source + = count-1;
Tmp_dest + = count-1;
While (count --)
* Tmp_dest -- = * tmp_source --;
}
// Overlaps 2: (Forward copy)
Else
{
While (count --)
{
* Tmp_dest ++ = * tmp_source ++;
}
}
}

Else
{
// Overlap 1: (reverse copy)
If (tmp_source <= tmp_dest & tmp_source + count> = tmp_dest)
{
Tmp_source + = count-1;
Tmp_dest + = count-1;
While (count --)
* Tmp_dest -- = * tmp_source --;
}
// Overlaps 2: (Forward copy)
Else
{
While (count --)
{
* Tmp_dest ++ = * tmp_source ++;
}
}
}
But after writing it, I found it silly .. Cannot the next else be written together with the previous one if? The complete code after optimization:

[Cpp]
Void * memmove (void * dest, const void * source, size_t count)
{
Assert (NULL! = Dest) & (NULL! = Source ));
Char * tmp_source, * tmp_dest;
Tmp_source = (char *) source;
Tmp_dest = (char *) dest;
 
If (tmp_source <= tmp_dest & tmp_source + count> = tmp_dest)
{
Tmp_source + = count-1;
Tmp_dest + = count-1;
While (count --)
* Tmp_dest -- = * tmp_source --;
}
Else
{
While (count --)
{
* Tmp_dest ++ = * tmp_source ++;
}
}
 
Return tmp_dest;
}

Void * memmove (void * dest, const void * source, size_t count)
{
Assert (NULL! = Dest) & (NULL! = Source ));
Char * tmp_source, * tmp_dest;
Tmp_source = (char *) source;
Tmp_dest = (char *) dest;

If (tmp_source <= tmp_dest & tmp_source + count> = tmp_dest)
{
Tmp_source + = count-1;
Tmp_dest + = count-1;
While (count --)
* Tmp_dest -- = * tmp_source --;
}
Else
{
While (count --)
{
* Tmp_dest ++ = * tmp_source ++;
}
}
 
Return tmp_dest;
}
 

 

Related Article

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.