个性化阅读
专注于IT技术分析

为什么strcpy和strncpy使用不安全?

点击下载

strcpy()函数

的strcpy()函数用于将源字符串复制到目标字符串。如果dest字符串的缓冲区大小大于src字符串, 则将src字符串复制到带有终止NULL字符的dest字符串。但是, 如果dest缓冲区较小, 则使用src它将复制内容而不会终止NULL字符。这些字符串可能不会重叠, 并且目标字符串必须足够大才能接收副本。

语法如下:

char *strcpy( char *dest, const char *src )

参数:此函数接受上述和以下所述的两个参数:

  • src:将被复制的字符串。
  • 目标:指向要在其中复制内容的目标数组的指针。

返回值:它返回一个指向目标字符串的指针。

// C Program  to illustrate the 
// strcpy() function in C/C++
#include <stdio.h>
#include <string.h>
int main()
{
     char src[] = "srcmini" ;
  
     // Here destination is large enough
     // to store the src with Null 
     // character at the end
     char dest[14];
  
     // copying src into dest.
     strcpy (dest, src);
     printf ( "Copied string: %s\n" , dest);
      
     return 0;
}

输出如下:

Copied string: srcmini

strcpy()问题:strcpy()函数未指定目标数组的大小, 因此缓冲区溢出通常会带来风险。使用strcpy()函数将大字符数组复制到较小的字符数组是危险的, 但是如果字符串适合, 那么就不值得冒险了。如果目标字符串的大小不足以存储源字符串, 则未指定或未定义strcpy()的行为。

// C Program  to illustrate the problem in 
// strcpy() function in C/C++
#include <stdio.h>
#include <string.h>
int main()
{
     char src[] = "srcmini" ;
  
     // Here destination is not large
     // enough to store the src. so the
     // behaviour of strcpy is unspecified.
     // program may crashed, but its
     // printing srcmini
     char dest[2];
      
     // copying src into dest.
     strcpy (dest, src);
     printf ( "Copied string: %s\n" , dest);
      
     return 0;
}

输出如下:

Copied string: srcmini

strncpy()函数

strncpy()函数类似于strcpy()函数, 除了最多复制n字节的src。如果src的前n个字符中没有NULL字符, 则放置在dest中的字符串将不会以NULL终止。如果src的长度小于n, 则strncpy()将另外的NULL字符写入dest以确保总共写入了n个字符。

语法如下:

char *strncpy( char *dest, const char *src, size_t n )

参数:此函数接受上述和以下所述的两个参数:

  • src:将被复制的字符串。
  • 目标:指向要在其中复制内容的目标数组的指针。
  • n:从src复制到dest的前n个字符。

返回值:它返回一个指向目标字符串的指针。

例子:

// C Program  to illustrate the 
// strcpy() function in C/C++
#include <stdio.h>
#include <string.h>
int main()
{
     char src[] = "srcmini" ;
      
     // The destination string size is 14.
     char dest[14];
      
     // copying n bytes of src into dest.
     strncpy (dest, src, 14);
     printf ( "Copied string: %s\n" , dest);
      
     return 0;
}

输出如下:

Copied string: srcmini

strncpy()问题:如果src的前n个字符中没有空字符, 则放置在dest中的字符串将不会以空字符结尾。因此, strncpy()不保证目标字符串将以NULL终止。未终止的strlen()字符串可能导致段错误。换句话说, C / C ++中的非终止字符串是一个等待炸毁代码的定时炸弹。

// C Program  to illustrate the problem in 
// strcpy() function in C/C++
#include <stdio.h>
#include <string.h>
int main()
{
     char src[] = "srcmini" ;
      
     // The destination sting size is 8
     // which is less than length of src.
     char dest[8];
      
     // copying 8 bytes of src into dest.
     // dest is not NULL terminated.
     strncpy (dest, src, 8);
      
     // using strlen function on non terminated.
     // string which can cause segfault.
     int len = strlen (dest);
      
     printf ( "Copied string: %s\n" , dest);
     printf ( "Length of destination string: %d\n" , len);
      
     return 0;
}

输出如下:

Copied string: geeksfor
Length of destination string: 8

现在, 下一个问题是, 有什么函数可以保证目标字符串将以NULL终止并且不会出现缓冲区溢出的情况?

因此, 以上问题的答案为”是”, ” stdio.h”库中有几个函数可确保满足以上条件。

  • snprintf
  • 结构

这两个函数都保证目标字符串将以NULL终止。snprintf()函数, strlcpy函数最多将dest_size-1个字符(dest_size是目标字符串缓冲区的大小)从src复制到dst, 并在必要时截断src。结果始终为空终止。该函数返回strlen(src)。缓冲区溢出可以按以下方式检查:

if (strlcpy(dst, src, dstsize) >= dest_size)
         return -1;

根据理智程度对功能进行排名:

strcpy < strncpy < snprintf < strlcpy

赞(0)
未经允许不得转载:srcmini » 为什么strcpy和strncpy使用不安全?

评论 抢沙发

评论前必须登录!