VS2005:declared deprecated warning问题

本文介绍在VS2005中如何解决因使用过时字符串函数导致的警告,并提供了一系列安全函数替代方案及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在VS2005下编译代码,有时会遇到类似如下的警告: warning C4996: 'strcat' was declared deprecated. 通常这类警告都是由于调用了字符串相关函数引起的。虽然这警告无伤大雅,仅仅只是说使用的函数已过时(deprecated)<需要用新的函数来替代>,但看着实在别扭,且看看ms为什么要设置成这样。

    搜索了一下ms的网站,找到了结果。ms认为以前的c/c++库中有一部分函数不够安全,希望程序员可以使用他们的替代安全库(Safe Library)来避免不必要的隐患。 整个原文请点击以下链接访问:Repel Attacks on Your Code with the Visual Studio 2005 Safe C and C++ Libraries

    在网上搜索到的最常用的解决方案,那就是定义 _CRT_SECURE_NO_DEPRECATE _SCL_SECURE_NO_DEPRECATE 来禁止vc2005对此产生警告(依然使用的是非安全库!显然并不是一个好的解决方案)。而且如果使用了ATL,则还需要定义 _ATL_SECURE_NO_DEPRECATE 使用了MFC则需要定义 _AFX_SECURE_NO_DEPRECATE
    
然而尽管如此,更好的解决方案只需要定义一个宏CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES, 那么VS将会自动替换使用他们的Safe Library来代替C/C++标准库(strcat将被strcat_f来取代)

    即使使用了_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES,代码将依旧不够安全, 对此,ms提出了如下10点建议:
      1.
不要认为 strcpy_s strncpy_s( 以及其他的字符串函数)(在空间不够的时候)会自动终止拷贝(truncate截断,不截断则意味着溢出).如果需要自动截断,请使用strncpy_s (同时使用_TRUNCATE作为长度参数)
      2.
记住fopen_s缺省是独占模式。如需共享使用文件,应该使用_sopen
      3.
别忘了_dupenv_s, 它将比_getenv_s更容易使用,因为它能自动分配一个正确长度的内存(buffer)
      4.
scanf_s中小心参数顺序。
      5.
确定printf_s中格式字符串的正确。
      6.
使用_countof(x)来取代sizeof(x)/sizeof(element). _countof将会正确的计算元素个数,而且如果x是一个指针,编译器将会发出一个警告(来提醒程序员,仅针对C++编译)
      7.
记住所有的sizes(大小,非长度)都是使用characters(字符,unicode下一个字符占2byte)作为单位,而不是bytes(字节).
      8.
记住所有的sizes(大小,非长度,缘由同上)包含了字符串结束符'/0'(即别忘了很多情况下size需要+1)
      9.
调试的时候监视数据0xfd (在调试版本下)0xfd将会被填充在数据(buffer,通常是字符串)的结尾处。如果运行非你所愿,可能会得到一个长度错误。
      10.
检查所有的错误。 许多新函数相比旧函数,能返回(表示)错误信息(的数值)

 

————————————————————————————————————————————————

     PS:定义宏_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES好像不能消除警告

 

 

1. gets_s()代替gets()
2.strcpy_s()来代替strcpy()
3.strncpy_s()代替strncpy()
4.sprintf_s()代替sprintf()
5.CString::Format(_T("字符转"))代替CString::Format("字符转")
6.strcat_s()代替strcat()
7.fopen_s()代替fopen()
8._vsnprintf_s()代替_vsnprintf()
9._ftime64_s()代替_ftime64()
10._get_tzname ()代替_tzname()
11._snwprintf_s()代替_snwprintf()
12.mbstowcs_s()代替mbstowcs()
13.wcstombs_s()代替wcstombs()
14.wcscat_s()代替wcscat()
15._wcsupr_s()代替_wcsupr()
16._wcslwr_s()代替_wcslwr() 

17._wtoi()代替atoi()

有这么多告警,怎么改/home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c: In function ‘rd_kafka_ssl_set_certs’: /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1609:17: warning: ‘ENGINE_load_ssl_client_cert’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1609 | r = ENGINE_load_ssl_client_cert( | ^ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:644:5: note: declared here 644 | int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s, STACK_OF(X509_NAME) *ca_dn, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c: In function ‘rd_kafka_ssl_ctx_term’: /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1683:9: warning: ‘ENGINE_free’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1683 | RD_IF_FREE(rk->rk_conf.ssl.engine, ENGINE_free); | ^~~~~~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:493:27: note: declared here 493 | OSSL_DEPRECATEDIN_3_0 int ENGINE_free(ENGINE *e); | ^~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c: In function ‘rd_kafka_ssl_ctx_init_engine’: /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1703:9: warning: ‘ENGINE_by_id’ is deprecated: Since OpenSSL 3. 0 [-Wdeprecated-declarations] 1703 | engine = ENGINE_by_id(rk->rk_conf.ssl.engine_id); | ^~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:336:31: note: declared here 336 | OSSL_DEPRECATEDIN_3_0 ENGINE *ENGINE_by_id(const char *id); | ^~~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1705:17: warning: ‘ENGINE_by_id’ is deprecated: Since OpenSSL 3 .0 [-Wdeprecated-declarations] 1705 | engine = ENGINE_by_id("dynamic"); | ^~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:336:31: note: declared here 336 | OSSL_DEPRECATEDIN_3_0 ENGINE *ENGINE_by_id(const char *id); | ^~~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1714:9: warning: ‘ENGINE_ctrl_cmd_string’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1714 | if (!ENGINE_ctrl_cmd_string(engine, "SO_PATH", | ^~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:479:5: note: declared here 479 | int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg, | ^~~~~~~~~~~~~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1716:17: warning: ‘ENGINE_free’ is deprecated: Since OpenSSL 3. 0 [-Wdeprecated-declarations] 1716 | ENGINE_free(engine); | ^~~~~~~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:493:27: note: declared here 493 | OSSL_DEPRECATEDIN_3_0 int ENGINE_free(ENGINE *e); | ^~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1723:9: warning: ‘ENGINE_ctrl_cmd_string’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1723 | if (!ENGINE_ctrl_cmd_string(engine, "LIST_ADD", "1", 0)) { | ^~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:479:5: note: declared here 479 | int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg, | ^~~~~~~~~~~~~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1724:17: warning: ‘ENGINE_free’ is deprecated: Since OpenSSL 3. 0 [-Wdeprecated-declarations] 1724 | ENGINE_free(engine); | ^~~~~~~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:493:27: note: declared here 493 | OSSL_DEPRECATEDIN_3_0 int ENGINE_free(ENGINE *e); | ^~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1731:9: warning: ‘ENGINE_ctrl_cmd_string’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1731 | if (!ENGINE_ctrl_cmd_string(engine, "LOAD", NULL, 0)) { | ^~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:479:5: note: declared here 479 | int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg, | ^~~~~~~~~~~~~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1732:17: warning: ‘ENGINE_free’ is deprecated: Since OpenSSL 3. 0 [-Wdeprecated-declarations] 1732 | ENGINE_free(engine); | ^~~~~~~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:493:27: note: declared here 493 | OSSL_DEPRECATEDIN_3_0 int ENGINE_free(ENGINE *e); | ^~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1739:9: warning: ‘ENGINE_init’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1739 | if (!ENGINE_init(engine)) { | ^~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:620:27: note: declared here 620 | OSSL_DEPRECATEDIN_3_0 int ENGINE_init(ENGINE *e); | ^~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1740:17: warning: ‘ENGINE_free’ is deprecated: Since OpenSSL 3. 0 [-Wdeprecated-declarations] 1740 | ENGINE_free(engine); | ^~~~~~~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:493:27: note: declared here 493 | OSSL_DEPRECATEDIN_3_0 int ENGINE_free(ENGINE *e); | ^~~~~~~~~~~ /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c: In function ‘rd_kafka_ssl_ctx_init’: /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:1980:9: warning: ‘ENGINE_free’ is deprecated: Since OpenSSL 3.0 [-Wdeprecated-declarations] 1980 | RD_IF_FREE(rk->rk_conf.ssl.engine, ENGINE_free); | ^~~~~~~~~~ In file included from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_conf.h:40, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_int.h:116, from /home/zwy/gitcode/dhmediaproxyserver/framework/3rdpart/librdkafka/src/rdkafka_ssl.c:36: /usr/include/openssl/engine.h:493:27: note: declared here 493 | OSSL_DEPRECATEDIN_3_0 int ENGINE_free(ENGINE *e); | ^~~~~~~~~~~
最新发布
07-23
home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:41:41: error: ‘AV_PIX_FMT_VDPAU_MPEG4’ was not declared in this scope; did you mean ‘AV_PIX_FMT_VDPAU’? 41 | # define TEST_PIX_FMT_RETURN(fmt) case AV_PIX_FMT_##fmt: return #fmt; | ^~~~~~~~~~~ /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:41:41: note: in definition of macro ‘TEST_PIX_FMT_RETURN’ 41 | # define TEST_PIX_FMT_RETURN(fmt) case AV_PIX_FMT_##fmt: return #fmt; | ^~~~~~~~~~~ /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp: In member function ‘void pangolin::FfmpegVideo::InitUrl(std::string, std::string, std::string, bool, int)’: /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:148:21: warning: ‘void av_register_all()’ is deprecated [-Wdeprecated-declarations] 148 | av_register_all(); | ^ In file included from /home/gc/Pangolin/include/pangolin/video/drivers/ffmpeg.h:43, from /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:28: /usr/local/include/libavformat/avformat.h:2050:6: note: declared here 2050 | void av_register_all(void); | ^~~~~~~~~~~~~~~ /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:148:21: warning: ‘void av_register_all()’ is deprecated [-Wdeprecated-declarations] 148 | av_register_all(); | ^ In file included from /home/gc/Pangolin/include/pangolin/video/drivers/ffmpeg.h:43, from /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:28: /usr/local/include/libavformat/avformat.h:2050:6: note: declared here 2050 | void av_register_all(void); | ^~~~~~~~~~~~~~~ /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:199:36: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations] 199 | if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) | ^~~~~ In file included from /home/gc/Pangolin/include/pangolin/video/drivers/ffmpeg.h:43, from /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:28: /usr/local/include/libavformat/avformat.h:880:21: note: declared here 880 | AVCodecContext *codec; | ^~~~~ /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:199:36: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations] 199 | if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) | ^~~~~ In file included from /home/gc/Pangolin/include/pangolin/video/drivers/ffmpeg.h:43, from /home/gc/Pangolin/src/video/drivers/ffmpeg.cpp:28: /usr/local/include/libavformat/avformat.h:880:21: note: declared here
03-29
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值