CMakeLists.txt 中设置warnings as errors,可以发现内存越界问题

博客讲述了如何通过在CMakeLists.txt中设置-Werror标志,将编译警告提升为错误,从而在C++代码中发现并解决内存越界问题。示例中,由于sprintf函数可能导致内存溢出,编译器报错提示-Werror=format-overflow=,强调了内存安全在软件开发中的重要性。

CMakeLists.txt 中设置warnings as errors,可以发现内存越界问题

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") #treating warnings as errors


            char tmpstr[30];
            frameId++;
            sprintf(tmpstr, "/data1/XXXXXXXXX/tmp/detect_%d.jpg", frameId);


报如下错误
/opt/intel/openvino/deployment_tools/open_model_zoo/demos/object_detection_demo_yolov3_async/main.cpp:180:5: error: '%d' directive writing between 1 and 10 bytes into a region of size 3 [-Werror=format-overflow=]

uilibTrafficTest.c: In function 'trafficTestRawPacketSet': uilibTrafficTest.c:121:127: error: pointer targets in passing argument 4 of 'tpConfig_objAddFieldStrForSet' differ in signedness [-Werror=pointer-sign] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_RAW_PACKET, packet), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'UCHAR * {aka unsigned char *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ cc1: all warnings being treated as errors CMakeFiles/uilib.dir/build.make:1313: recipe for target 'CMakeFiles/uilib.dir/uilibTrafficTest.c.o' failed make[4]: *** [CMakeFiles/uilib.dir/uilibTrafficTest.c.o] Error 1 make[4]: *** Waiting for unfinished jobs.... [ 89%] Building C object CMakeFiles/uilib.dir/lldp/uiLldpParamCheck.c.o CMakeFiles/Makefile2:107: recipe for target 'CMakeFiles/uilib.dir/all' failed make[3]: *** [CMakeFiles/uilib.dir/all] Error 2 Makefile:132: recipe for target 'all' failed make[2]: *** [all] Error 2 package/tplink/tplink-generic.mk:289: recipe for target '/project/fep_source_2/buildroot/build/ac5x_pro/build/tplink/uilib-1.0/.stamp_built' failed make[1]: *** [/project/fep_source_2/buildroot/build/ac5x_pro/build/tplink/uilib-1.0/.stamp_built] Error 2 Makefile:84: recipe for target '_all' failed make: *** [_all] Error 2 解释下这个报错
最新发布
10-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值