启用constVariablePointer检查并修复6处指针const化
.pre-commit-config.yaml: 移除 --suppress=constVariablePointer net_select.c: 5 处 p_sock 改 const net_sock_t *(仅只读使用) net_socket.c: net_getsockname 的 p_sock 改 const(只读字段) docs/代码规范/cppcheck使用说明.md: 同步抑制表与处理记录
This commit is contained in:
@@ -50,7 +50,7 @@ repos:
|
||||
'--suppress=unusedFunction', '--suppress=unusedStructMember',
|
||||
'--suppress=noExplicitConstructor', '--suppress=useStlAlgorithm',
|
||||
'--suppress=constParameterPointer', '--suppress=constParameterCallback',
|
||||
'--suppress=constVariablePointer', '--suppress=redundantAssignment',
|
||||
'--suppress=redundantAssignment',
|
||||
'-I', 'Inc', '-I', 'App', '-I', 'Drivers/BSP',
|
||||
'-I', 'Drivers/STM32F4xx_HAL_Driver/Inc', '-I', 'Drivers/CMSIS/Include',
|
||||
'-I', 'Middlewares/Third_Party/FreeRTOS/Source/include']
|
||||
|
||||
@@ -78,7 +78,7 @@ int net_select(int nfds, net_fd_set *readfds, net_fd_set *writefds,
|
||||
|
||||
/* 检查哪个 Socket 有事件 */
|
||||
for (i = 0; i < nfds && i < NET_MAX_SOCKETS; i++) {
|
||||
net_sock_t *p_sock = net_get_sock(i);
|
||||
const net_sock_t *p_sock = net_get_sock(i);
|
||||
|
||||
if (NULL == p_sock) {
|
||||
continue;
|
||||
@@ -178,7 +178,7 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
|
||||
continue;
|
||||
}
|
||||
|
||||
net_sock_t *p_sock = net_get_sock(fds[i].fd);
|
||||
const net_sock_t *p_sock = net_get_sock(fds[i].fd);
|
||||
|
||||
if (NULL == p_sock) {
|
||||
fds[i].revents |= NET_POLLNVAL;
|
||||
@@ -247,7 +247,7 @@ int net_poll_events(net_pollfd *fds, int nfds, int timeout) {
|
||||
* 2. 已建立连接且接收缓冲区长度大于 0 时视为可读
|
||||
*/
|
||||
static int check_socket_readable(int sockfd) {
|
||||
net_sock_t *p_sock;
|
||||
const net_sock_t *p_sock;
|
||||
|
||||
p_sock = net_get_sock(sockfd);
|
||||
if (NULL == p_sock) {
|
||||
@@ -277,7 +277,7 @@ static int check_socket_readable(int sockfd) {
|
||||
* 函数说明:1. 仅已建立连接且 send_ready 标志置位的 Socket 视为可写
|
||||
*/
|
||||
static int check_socket_writable(int sockfd) {
|
||||
net_sock_t *p_sock;
|
||||
const net_sock_t *p_sock;
|
||||
|
||||
p_sock = net_get_sock(sockfd);
|
||||
if (NULL == p_sock) {
|
||||
@@ -304,7 +304,7 @@ static int check_socket_writable(int sockfd) {
|
||||
* 2. 未分配(net_get_sock 返回 NULL)的 Socket 视为错误
|
||||
*/
|
||||
static int check_socket_error(int sockfd) {
|
||||
net_sock_t *p_sock;
|
||||
const net_sock_t *p_sock;
|
||||
|
||||
/* 无效 fd 视为错误 */
|
||||
if ((0 > sockfd) || (NET_MAX_SOCKETS <= sockfd)) {
|
||||
|
||||
@@ -1848,7 +1848,7 @@ int net_getsockname(int sockfd, struct net_sockaddr *addr, int *addrlen) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
net_sock_t *p_sock = &s_net_socks[sockfd];
|
||||
const net_sock_t *p_sock = &s_net_socks[sockfd];
|
||||
if (!p_sock->in_use && p_sock->state == NET_SOCK_STATE_CLOSED) {
|
||||
s_net_errno = NET_ERR_BADF;
|
||||
return -1;
|
||||
|
||||
@@ -74,7 +74,7 @@ cppcheck 是开源 C/C++ 静态分析工具,**不运行代码**,通过语法
|
||||
|
||||
| 规则 | 检查什么 | 备注 |
|
||||
|---|---|---|
|
||||
| `constVariable` / `constVariablePointer` | 变量/指针可加 const 防误改 | `constVariablePointer` 当前**已抑制**(改动量大)|
|
||||
| `constVariable` / `constVariablePointer` | 变量/指针可加 const 防误改 | 已全库启用并修复 |
|
||||
| `constParameter` / `constParameterPointer` / `constParameterCallback` | 参数可加 const | 回调相关两条**已抑制**(HAL 签名不可改)|
|
||||
| `variableScope` | 变量作用域可缩小(声明移入实际使用的最内层块)| 已全库修复 |
|
||||
| `functionStatic` / `staticFunction` | 函数可加 static 限制可见性 | 本库函数多被外部引用,命中少 |
|
||||
@@ -137,9 +137,6 @@ cppcheck 是开源 C/C++ 静态分析工具,**不运行代码**,通过语法
|
||||
--suppress=noExplicitConstructor # C++ 兼容,不影响纯 C
|
||||
--suppress=useStlAlgorithm # C++ STL 相关,不影响纯 C
|
||||
|
||||
# --- 暂时抑制(噪音多,后续可移除) ---
|
||||
--suppress=constVariablePointer # 局部指针加 const 是好习惯,但当前改动量大
|
||||
|
||||
# --- 头文件路径 ---
|
||||
-I Inc -I App -I Drivers/BSP
|
||||
-I Drivers/STM32F4xx_HAL_Driver/Inc -I Drivers/CMSIS/Include
|
||||
@@ -172,7 +169,6 @@ args: ['--enable=warning,performance,portability,style', '--error-exitcode=1',
|
||||
|---|---|---|
|
||||
| `constParameterPointer` | HAL 回调签名由驱动框架定义,用户不能改 | 永久 |
|
||||
| `constParameterCallback` | 同上,回调函数指针类型受限 | 永久 |
|
||||
| `constVariablePointer` | 局部指针加 const 是好习惯,但当前代码量大、改动多 | 暂时 |
|
||||
| `unusedFunction` | CubeMX 生成的弱符号、第三方库回调被误报 | 永久 |
|
||||
| `unusedStructMember` | HAL 寄存器位域按位操作,cppcheck 不理解 | 永久 |
|
||||
| `redundantAssignment` | HAL 清标志宏(如 `__HAL_SPI_CLEAR_OVRFLAG`)读寄存器覆盖旧值,cppcheck 不理解"读即目的" | 永久 |
|
||||
@@ -233,6 +229,7 @@ if (day_count < diy) break;
|
||||
| `redundantAssignment` | 重复赋值覆盖旧值 | ch395f.c | ✅ 已抑制(HAL 清标志宏读寄存器即目的,误报) |
|
||||
| `unreadVariable` | 变量赋值后未读取 | gd5f2gq5ue.c, time_sync.c, ringbuf.c | ✅ 已修复(移除冗余初始化) |
|
||||
| `variableScope` | 变量作用域可缩小 | sys_clock.c, net_socket.c 等 18 处 | ✅ 已修复(声明移入实际使用块) |
|
||||
| `constVariablePointer` | 指针可加 const 防误改 | net_select.c, net_socket.c 共 6 处 | ✅ 已修复(`p_sock` 改为 `const net_sock_t *`) |
|
||||
| `knownConditionTrueFalse` | `day_count < diy` 被判恒假(闰年 `diy=366` 分支未跟踪,误报) | sys_clock.c:108 | ✅ 已行内抑制(`// cppcheck-suppress`) |
|
||||
|
||||
> 注:`knownConditionTrueFalse` 属真误报——闰年分支 `diy=366` 时 `day_count==365` 的 break 有意义,cppcheck 数据流分析未覆盖该分支。已用行内抑制处理,不影响该规则在其他文件上的正常检查。
|
||||
|
||||
Reference in New Issue
Block a user