Usage of strcpy, strncpy, and strlcpy

Source: Internet
Author: User
Tags truncated

Many people already know how to use strncpy to replace strcpy to prevent Buffer Overflow. However, if you still need to consider the running efficiency, strlcpy may be a better way.

1. strcpy

We know that strcpy is judged based on \ 0. If the to space is not enough, it will cause buffer overflow. General implementation of strcpyCodeAs follows (from OpenBSD 3.9 ):

Char *

Strcpy (char * To, const char * From)

{

Char * save =;

For (; (* To = * From )! = '\ 0'; ++ from, ++ );

Return (SAVE );

}

But generally, our from is derived from user input, which is probably a very large string, so strcpy is not safe enough.

2. strncpy

In ansi c, the secure version of strcpy is strncpy.

Char * strncpy (char * S1, const char * S2, size_t N );

But strncpy's behavior is very strange (not in line with our usual habits ). The standard stipulates that N is not sizeof (S1), but the number of Char to be copied. One of the most common problems is that strncpy does not help you ensure \ 0 is over.

Char Buf [8];

Strncpy (BUF, "abcdefgh", 8 );

Check thisProgram, The Buf will be filled with "abcdefgh", but there is no \ 0 Terminator.

In addition, if S2 has less content and N is large, strncpy will fill the space between them with \ 0. This introduces another efficiency problem, as shown below:

Char Buf [80];

Strncpy (BUF, "abcdefgh", 79 );

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

The standard usage of strncpy is: (manually write \ 0)

Strncpy (path, SRC, sizeof (PATH)-1 );

Path [sizeof (PATH)-1] = '\ 0 ';

Len = strlen (PATH );

3. strlcpy

// Copy SRC to string DST of size siz. At most siz-1 characters

// Will be copied. Always NUL terminates (unless siz = 0 ).

// Returns strlen (SRC); If retval> = siz, truncation occurred.

Size_t

Strlcpy (char * DST, const char * SRC, size_t siz );

To use strlcpy, we do not need to manually take charge of \ 0. We only need to inform strlcpy of sizeof (DST:

Strlcpy (path, SRC, sizeof (PATH ));

Len = strlen (PATH );

If (LEN> = sizeof (PATH ))

Printf ("src is truncated .");

In addition, strlcpy returns strlen (STR), so we can easily determine whether the data is truncated.

[* A little history *]

Strlcpy does not belong to ansi c and is not yet a standard.

Strlcpy comes from OpenBSD 2.4. Later, many libc Unix-like systems have added strlcpy functions. I personally found strlcpy in FreeBSD and Linux. (Glibc is used in Linux, and strlcpy is included in glibc, so all Linux versions should also have strlcpy)

But in windows, there is no strlcpy, which corresponds to the strcpy_s function.

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.