Files
STM32F4-Base/test/host/sntp/stub/net_socket.h
T
wandering1 04ee5867b4 fix(net): 建立网络 fd 失效值契约,杜绝零初始化误判
根因:静态 fd 零初始化为 0,而 0 是合法 socket 句柄;判据用 >=0
会把"未初始化"误判成"有效",关闭时误伤他人 fd。本次把该修复
契约化、机械化,而不是再补一个点。

- 新增 net_fd.h 定义唯一失效值 NET_FD_INVALID(-1),全仓零依赖复用
- fd 判据只用 ==/!= NET_FD_INVALID,禁 >=0/<0/数值形态/if(fd)
- 网络层内部用 NET_FD_IN_RANGE 做范围校验,上界不外泄
- 主机 fake 改 0 基并跟踪已分配 fd,补冷启动回归(变异测试可捕获)
- 新增 net-handle 静态门禁(钩子 53)+ 契约文档 + 审计 + 最小加固

验证:net-handle 487 文件 0 违规;8 个主机套件全绿;Keil 0 错误 0 警告;
审计 3 确认/5 疑似/35 安全,无第二处生产误判。
2026-11-18 22:20:00 +08:00

30 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 主机测试固件(test/host/sntp):net_socket.h 替身——UDP 客户端最小子集。
* 合同与真机 Drivers/BSP/NET/net_socket.h(PR #74 后)一致:
* net_sendto 返回 >0 = 实发字节数(须恰等 48),-1 = 错误(net_get_errno 可查)
* net_recvfrom_to >0 交付 / -2 片内无数据(不置 errno)/ -1 错误 / 0 防御
* 载荷/源地址/返回值由测试脚本注入(sntp_test.c 的 fake 实现)。 */
#ifndef SNTP_TEST_NET_SOCKET_H
#define SNTP_TEST_NET_SOCKET_H
#include "net_fd.h" /* NET_FD_INVALID:与生产同一份契约(stub 不复制失效值) */
#include "task.h"
#include "net_types.h"
/* 脚本注入 token(fake 队列 kind 用):数值即 fake net_recvfrom_to 的返回值 */
#define NET_RECV_DATA 1
#define NET_RECV_TIMEOUT (-2)
#define NET_RECV_CONNCLOSE 0
#define NET_RECV_ERR (-1)
int net_socket(int domain, int type);
int net_bind(int sockfd, const struct net_sockaddr *addr, int addrlen);
int net_sendto(int sockfd, const void *buf, int len, int flags,
const struct net_sockaddr *dest, int addrlen);
int net_recvfrom_to(int sockfd, void *buf, int len, uint32_t timeout_ms,
struct net_sockaddr *src, int *addrlen);
int net_close(int sockfd);
int net_get_errno(void);
uint32_t net_get_generation(void);
#endif /* SNTP_TEST_NET_SOCKET_H */