本文介绍了C ++'strcpy'给出警告(C4996)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此警告,但所有功能都正常工作。

I am getting this warning but all functions working properly .

这是什么意思?

'strcpy': This function or variable may be unsafe.
Consider using strcpy_s instead. To disable deprecation,
use _CRT_SECURE_NO_WARNINGS. See online help for details.


推荐答案

此函数(strcpy)事实上没有边界检查并且可能导致缓冲区溢出。 (实际上,strcpy臭名昭着的溢出漏洞,所有的程序员都避免了 - 或者至少应该避免它)。建议是使用一个安全函数,该函数考虑目标缓冲区的大小以避免溢出。你也可以使用strncpy(但小心!)。您的代码没有问题,即函数将按照您所说的运行,但尝试将大于目标缓冲区的缓冲区作为输入。该函数将溢出目标缓冲区。另请勾选这里

This function (strcpy) is considered unsafe due to the fact that there is no bounds checking and can lead to buffer overflow. (Actually strcpy is infamous for overflow exploits and all programmers avoid it-or at least should avoid it). The advice is to use a safe function which takes into account the size of the destination buffer to avoid overflow. You could also use strncpy (BUT with caution!). There is no problem with your code, i.e. the functions will run as you say but try giving as input a buffer that is larger than the destination buffer. The function will overflow the destination buffer. Check this also link text

这篇关于C ++'strcpy'给出警告(C4996)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:33