Similarities and differences of strcpy, strcpy_s, strncpy and strlcpy in C + +

Source: Internet
Author: User
Tags truncated

strncpyUsage: It differs from strcpy copying n characters instead of copying all characters (including endings ‘\0‘ ).

Function Prototypes:char * strncpy(char *dst,const char * src, int n)

When src the length is less than n , the dst inside of the non-duplicated space is ‘\0‘ filled with. Otherwise, copy n characters to dst , no add ‘\0‘ . It's important to look at the end of the string to dst handle the addition ‘\0‘ .

strcpy, strncpy strlcpy The use of a lot of people already know strncpy to use overrides strcpy to prevent buffer out of bounds. But it might strlcpy be a better way to think about the efficiency of the operation.

strcpy

We know that the strcpy basis \0 as the end of the judgment, will automatically in the buffer string after the addition \0 , if to the space is not enough, it will be caused buffer overflow . strcpyThe general implementation code is as follows (from OpenBSD 3.9 ):

char *strcpy(charconstchar *from){       char *save = to;       for (; (*to = *from‘\0‘; ++from, ++to);       return(save);}

But usually, we from are all derived from the user's input, which is probably a very large string, and therefore strcpy not secure enough.

strcyp_s

It strcpy() should function the same as the function. strcpyfunction, like gets a function, has no method to guarantee a valid buffer size, so it can only assume that the buffer is large enough to hold the string to be copied. This leads to unpredictable behavior when the program is running. strcpy_sthese unpredictable behaviors can be avoided.

This function can be used with two parameters and three parameters, as long as the buffer size can be guaranteed.

Three parameters:

errno_t strcpy_s(charconstchar *strSource);

Two parameters:

errno_t strcpy_s(charconstchar// C++ only 

It is important to note that if you need to use a version of two parameters, the strDestination space you point to must be statically allocated, not the dynamically new out-of-heap memory.

strncpy

In ANSI C , strcpy the security version is strncpy .

char *strncpy(charconstchar *s2, size_t n);

But strncpy the behavior is very bizarre (not in accordance with our usual habits). The standard specification n is not sizeof(s1) , but the number of copies to be copied char . One of the most common problems is strncpy not to help you to guarantee the \0 end.

char buf[8];strncpy( buf, "abcdefgh", 8 );

Look at this program, it buf will be "abcdefgh" filled, but there is no Terminator \0 .
In addition, if s2 the content is relatively small, and n relatively large, strncpy will be the space between the \0 fill. There is an efficiency problem, as follows:

char buf[80];strncpy( buf, "abcdefgh", 79 );

The above strncpy will fill in 79 char, not just the one "abcdefgh" itself.

strncpyThe standard usage is: (hand-written \0 )

strncpysizeof1);path[sizeof1‘\0‘strlen(path);
strlcpy

Copied src to the size of the size destination buffer, the maximum number of copies siz-1 , the first size position will be added ‘\0‘ , the execution will be returned after the end strlen(src) . So, if retval >= siz , then because the destination buffer is too small to src be truncated.

size_t strlcpy(charconstchar *src, size_t siz);

Use strlcpy , we do not need to be responsible for the manual \0 , only need to sizeof(dst) tell strlcpy :

sizeofstrlen(path);ifsizeof(path) )       printf("src is truncated.");

And strlcpy The return is strlen(str) , therefore we are also very convenient to determine whether the data is truncated, but also to the program buried a ray, so, strlcpy does not belong ANSI C to, so far is not the standard.

Similarities and differences of strcpy, strcpy_s, strncpy and strlcpy in C + +

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.