Strcpy, strncpy, and strlcpy, how to retain one byte when one more bit is added

Source: Internet
Author: User
Tags truncated

When an array is passed as a parameter to a function, it is a pointer rather than an array, and the first address of the array is passed, for example:
Void func (char STR [1, 100])
{......
}

Then sizeof (STR) = 4
When the STR array is declared as a virtual parameter, sizeof (STR) = 4 (pointer size)

 

 

Strncat

Prototype: extern char * strncat (char * DEST, char * SRC, int N );
Usage: # include <string. h>
Function: add the first n characters of the string referred to by Src to the end of DeST (overwrite '/0' at the end of DEST) and add'/0 '.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.

 

 

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. The general implementation code of strcpy is as follows (from OpenBSD 3.9 ):

Char *
Strcpy (char * To, const char * From)
{
Char * save =;

For (; (* To = * From )! = ''; ++ 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 guarantee the end.

Char Buf [8];
Strncpy (BUF, "abcdefgh", 8 );

Looking at this program, the Buf will be filled with "abcdefgh", but there is no Terminator.

In addition, if the content of S2 is relatively small, and N is relatively large, strncpy will fill the space between them. 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: (written manually)

Strncpy (path, SRC, sizeof (PATH)-1 );
Path [sizeof (PATH)-1] = '';
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 );

When strlcpy is used, 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 UNIX-like libc systems have added the strlcpy function.
Strlcpy is found in FreeBSD and Linux. (Glibc is used in Linux. If there is strlcpy in glibc, all
Strlcpy should also be available in Linux)

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

 

 

 

VB-> size = Vb-> width * Vb-> height * (BUF-> FMT-> depth + 7)> 3 );

The buffer size is calculated based on the screen width, height, and depth. But why should I add 7 to depth?

Because if depth has an remainder of 8, one more byte can be retained.

Note:

1. shifts three places to the right, which indicates dividing by 8.

2. In fact, this depth has a remainder of 8. For example, rgb444 only has 12 colors. However, it must be stored in two bytes.

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.