Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
源码分析
AST静态代码扫描
在RT-Thread的scons基础上,使用clang-tidy做静态分析
5.00
发布于 2022-09-20 00:16:09 浏览:2008
订阅该版
[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
回答
4482
被采纳
381
关注TA
发私信
相关文章
1
kawaii mqtt 严谨使用注意事项,求助
2
关于阅读 device.c 的几点疑惑
3
rt thread studio软件中怎么设置添加pc lint工具
4
mem.c 静态代码pclint检查报错
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
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
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
出出啊
1518
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
813
个答案
177
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
5
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部