天天看点

warning C4996: 'gets': This function or variable may be unsafe. Consider using gets_s instead.

To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Significant enhancements have been made to make the C Run-Time Libraries (CRT)  more secure. Many C Run-Time Libraries (CRT)  functions now have more secure versions. If a new secure function exists, the older, less secure version is marked as deprecated and the new version has the _s ( "secure ") suffix.

It should be noted that in this context, "deprecated " just means that a function 's use is not recommended; it does not indicate that the function is scheduled to be removed from the C Run-Time Libraries (CRT) .

It should also be noted that the secure functions do not prevent or correct security errors; rather, they catch errors when they occur. They perform additional checks for error conditions, and in the case of an error, they invoke an error handler (see Parameter Validation).

For example, the strcpy function has no way of telling if the string that it 's copying is too big for its destination buffer. However, its secure counterpart, strcpy_s, takes the size of the buffer as a parameter, so it can determine if a buffer overrun will occur. If you use strcpy_s to copy eleven characters into a ten-character buffer, that is an error on your part; strcpy_s cannot correct your mistake, but it can detect your error and inform you by invoking the invalid parameter handler.

一些重大改进使得C运行时库更加安全。许多 C运行时库函数C Run-Time Libraries (CRT)现在有了更安全的版本。

如果一个新的安全函数的存在,则旧的不太安全的版本会被标记为过时,而新版本后面会添加_s(“安全”)后缀。

应当指出的是,在这方面, “过时”的意思只是说,不推荐使用该函数的;它并不表明,该函数将从C运行时库C Run-Time Libraries (CRT)中去除 。

还应当指出,安全版本的函数并不能防止或纠正安全错误,它们只是在错误发生时进行捕捉。他们执行额外的错误条件检查,并在发生错误时,他们援引的错误处理程序(见参数验证) 。

例如,strcpy函数无法知道如果,它将要复制的字符串对于目的地缓冲区来说是否太大。但是,它对应的安全版本函数, strcpy_s ,将会把需要缓冲区的大小作为一个参数,因此它可以决定是否将出现缓冲区溢出情况。如果您使用strcpy_s 将11个字符复制到10个字符大小的缓冲区中,将会报出一个错误; strcpy_s虽不能纠正该错误,但它可以检测你的错误,并通过援引无效的参数处理程序告诉您该错误。