Linux cppcheck使用

本文详细介绍Cppcheck这一强大的C/C++静态代码分析工具。它能检测编译器可能忽略的潜在bug,如未使用的变量、越界访问、空指针引用等。文章通过实例展示如何安装并使用Cppcheck,帮助开发者提升代码质量。

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

cppcheck Tool for static C/C++ code analysis

描述

        Cppcheck is a command-line tool that tries to detect bugs that your C/C++ compiler doesn't see. It is versatile, and can check non-standard code including
       various compiler extensions, inline assembly code, etc. Its internal preprocessor can handle includes, macros, and several preprocessor commands. While
       Cppcheck is highly configurable, you can start using it just by giving it a path to the source code.

用法

虚拟机里面安装命令:yum install cppcheck,使用方法:cppcheck --enable=all [files or paths],重点看error打印

提示

风格:

ubus.c:214]: (style) The scope of the variable 'ret' can be reduced.
ubus.c:219]: (style) Variable 'ret' is assigned a value that is never used.
gnss_hal.c:230]: (style) Variable 'bpos' is assigned a value that is never used.

ret变量没有使用

错误:

ubus.c:264]: (error) Common realloc mistake: 'main_object_methods' nulled but not freed upon failure

指针没有判断NULL,且没有地方进行free

service_ubus.c:185]: (error) Buffer is accessed out of bounds: data

buffer明显越界

upgrade_handler.c:106]: (error) Null pointer dereference: path

有访问空指针的风险

timing_pal.c:1480]: (error) Division by zero.

有除0的风险

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值