天天看點

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雖不能糾正該錯誤,但它可以檢測你的錯誤,并通過援引無效的參數處理程式告訴您該錯誤。