Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
源码分析
在RT-Thread的scons基础上,使用clang-tidy做静态分析
5.00
发布于 2022-09-20 00:16:09 浏览:1183
订阅该版
[tocm] ## 参考文档 - https://clang.llvm.org/extra/clang-tidy/ ### 测试代码 先写个简单的代码 main.c ```c #include
#include
#include
int main(int argc, char *argv[]) { char *tmp = (char *)malloc(argc); if(tmp) { strncpy(tmp, argv[0], argc); printf("%s", tmp); if(argc > 2) { free(tmp); } } printf("hello world!\n"); return 0; } ``` 使用clang-tidy分析一下 ``` # apt install -y clang-tidy # clang-tidy main.c Error while trying to load a compilation database: Could not auto-detect compilation database for file "main.c" No compilation database found in /work/analyzer or any parent directory fixed-compilation-database: Error while opening fixed database: No such file or directory json-compilation-database: Error while opening JSON database: No such file or directory Running without flags. 2 warnings generated. /work/analyzer/main.c:12:3: warning: Call to function 'strncpy' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'strncpy_s' in case of C11 [clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling] strncpy(tmp, argv[0], argc); ^~~~~~~ /work/analyzer/main.c:12:3: note: Call to function 'strncpy' is insecure as it does not provide security checks introduced in the C11 standard. Replace with analogous functions that support length arguments or provides boundary checks such as 'strncpy_s' in case of C11 strncpy(tmp, argv[0], argc); ^~~~~~~ /work/analyzer/main.c:21:2: warning: Potential leak of memory pointed to by 'tmp' [clang-analyzer-unix.Malloc] printf("hello world!\n"); ^ /work/analyzer/main.c:8:22: note: Memory is allocated char *tmp = (char *)malloc(argc); ^~~~~~~~~~~~ /work/analyzer/main.c:10:5: note: Assuming 'tmp' is non-null if(tmp) ^~~ /work/analyzer/main.c:10:2: note: Taking true branch if(tmp) ^ /work/analyzer/main.c:15:6: note: Assuming 'argc' is <= 2 if(argc > 2) ^~~~~~~~ /work/analyzer/main.c:15:3: note: Taking false branch if(argc > 2) ^ /work/analyzer/main.c:21:2: note: Potential leak of memory pointed to by 'tmp' printf("hello world!\n"); ^ ``` ## compile_commands.json 还是有点用的,但我们RT-Thread项目中代码那么多,一个一个输入太麻烦了。 而且我们代码多是arm和gcc的,而开发机多是x86,clang默认参数也分析不了。 刚才分析结果第1行就有提示,没有找到`compilation database`,其实scons就可以生成 需要比较新的版本 ``` python3 -m pip install scons python3 -m SCons -v SCons by Steven Knight et al.: SCons: v4.0.1.c289977f8b34786ab6c334311e232886da7e8df1, 2020-07-17 01:50:03, by bdbaddog on ProDog2020 SCons path: ['/usr/lib/python3/dist-packages/SCons'] Copyright (c) 2001 - 2020 The SCons Foundation ``` 然后更新下Scons脚本,让生成compile_commands.json ``` # git diff SConstruct +env.Tool('compilation_db') +env.CompilationDatabase() # make a building DoBuilding(TARGET, objs) ``` ## run-clang-tidy 然后使用 run-clang-tidy 就可以自动分析所有的源代码了。 ## 输出html 生成的结果是文本,查看起来还是比较费力的,目前有个简单的转换为html格式, 虽然也比较简陋,但比文本还是方便多了。 ``` run-clang-tidy > clang_tidy_analyzer.txt pip3 install clang-html clang-tidy-html clang_tidy_analyzer.txt ```
9
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
aozima
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
文章
28
回答
4450
被采纳
371
关注TA
发私信
相关文章
1
kawaii mqtt 严谨使用注意事项,求助
2
关于阅读 device.c 的几点疑惑
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
机器人操作系统 (ROS2) 和 RT-Thread 通信
4
五分钟玩转RT-Thread新社区
5
国产MCU移植系列教程汇总,欢迎查看!
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
LWIP
Env
AT
SPI
Bootloader
FinSH
ART-Pi
CAN总线
Hardfault
USB
文件系统
RT-Thread
DMA
SCons
线程
MQTT
RT-Thread Nano
STM32
RTC
rt-smart
ESP8266
flash
ota在线升级
WIZnet_W5500
FAL
I2C
packages_软件包
UART
cubemx
freemodbus
潘多拉开发板_Pandora
定时器
BSP
PWM
ADC
socket
中断
rt_mq_消息队列_msg_queue
keil_MDK
SDIO
Debug
AB32VG1
MicroPython
C++_cpp
编译报错
msh
ulog
QEMU
本月问答贡献
出出啊
1501
个答案
338
次被采纳
小小李sunny
1390
个答案
276
次被采纳
张世争
715
个答案
157
次被采纳
crystal266
522
个答案
153
次被采纳
whj467467222
1216
个答案
146
次被采纳
本月文章贡献
出出啊
1
篇文章
1
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
2
篇文章
2
次点赞
crystal266
2
篇文章
5
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部