Strcpy_s, wcscpy_s, _ mbscpy_s overlap

Source: Internet
Author: User

When we see these functions, we feel that these functions are no longer common, but we have to patiently read the msdn explanation. Pay attention to one sentence:

The strcpy_s function copies the contents
In the address of strsource, including the terminating
NULL character, to the location specified by strdestination.
The destination string must be large enough to hold the source string, including the terminating null character. The behavior of strcpy_s is
Undefined if the source and destination strings overlap.

The above red text is worth noting, because the bug/crash caused by undefined behavior is often difficult to reproduce, resulting in system instability:

For example, this Code:

// Trim the left side of a string and trim characters such as spaces

Bool testcpystring: trimleftinvalidchars (const tchar * stroldline, tchar * strnewline, int Len)
{
If (null =
Stroldline | null = strnewline) return false;
_ Tcscpy_s (strnewline, Len,
Stroldline); // copy the original string to a new location (strnewline)

Tchar * pchar = NULL;
Pchar = strnewline; // remember the current position

// Jump to the character to be trimmed, so that we can reach the position to be copied.
While (* pchar = _ T ('') | * pchar = _ T ('\ t') | * pchar = _ T (''))
{
Pchar ++;
}

// Copy the string to be copied
_ Tcscpy_s (strnewline, Len, pchar); // here the source string and target string overlap problem occurs, so the consequences are unknown.

Return true;
}


After the change is:


Bool testcpystring: trimleftinvalidchars (const tchar * stroldline, tchar * strnewline, int Len)
{
If (null = stroldline | null = strnewline) return false;

Tchar * pchar = NULL;
Pchar = const_cast <tchar *> (stroldline); // remember the current position

// Jump to the character to be trimmed, so that we can reach the position to be copied.
While (* pchar = _ T ('') | * pchar = _ T ('\ t') | * pchar = _ T (''))
{
Pchar ++;
}

// Copy the string to be copied
_ Tcscpy_s (strnewline, Len, pchar); // The Source string and target string do not overlap.

Return true;
}

Sharing!


Related Keywords:

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.