fix: 消除编译警告并修复测试清理逻辑

- 将 config.c 和 proxy.c 中的 strncpy 替换为 memcpy + 显式 null 结尾,
  消除 GCC -Wstringop-truncation 警告
- 修复 integration_test.sh 的 kill_server 函数,补充对 -c 配置方式启动的
  服务器进程的清理,避免端口占用导致后续测试失败

编译零警告,142 个单元测试 + 80 项集成测试全部通过
This commit is contained in:
xfy911 2026-06-07 06:06:59 +08:00
parent cc06fc073f
commit 1b930aa5e1
3 changed files with 16 additions and 4 deletions

View File

@ -382,8 +382,14 @@ bool config_load_from_file(const char *path, cocoon_config_t *config) {
}
}
if (prefix[0] && target[0] && config->num_proxies < COCOON_MAX_PROXY_RULES) {
strncpy(config->proxies[config->num_proxies].prefix, prefix, sizeof(config->proxies[0].prefix)-1);
strncpy(config->proxies[config->num_proxies].target, target, sizeof(config->proxies[0].target)-1);
size_t prefix_len = strlen(prefix);
if (prefix_len >= sizeof(config->proxies[0].prefix)) prefix_len = sizeof(config->proxies[0].prefix) - 1;
memcpy(config->proxies[config->num_proxies].prefix, prefix, prefix_len);
config->proxies[config->num_proxies].prefix[prefix_len] = '\0';
size_t target_len = strlen(target);
if (target_len >= sizeof(config->proxies[0].target)) target_len = sizeof(config->proxies[0].target) - 1;
memcpy(config->proxies[config->num_proxies].target, target, target_len);
config->proxies[config->num_proxies].target[target_len] = '\0';
config->num_proxies++;
}
} else {

View File

@ -78,8 +78,10 @@ bool proxy_add_rule(cocoon_proxy_config_t *cfg, const char *prefix, const char *
rule->target_host[host_len] = '\0';
rule->target_port = (uint16_t)atoi(colon + 1);
} else {
strncpy(rule->target_host, host_port, sizeof(rule->target_host) - 1);
rule->target_host[sizeof(rule->target_host) - 1] = '\0';
size_t host_len = strlen(host_port);
if (host_len >= sizeof(rule->target_host)) host_len = sizeof(rule->target_host) - 1;
memcpy(rule->target_host, host_port, host_len);
rule->target_host[host_len] = '\0';
rule->target_port = https ? 443 : 80;
}

View File

@ -27,6 +27,10 @@ kill_server() {
if [[ -n "$pids" ]]; then
echo "$pids" | xargs kill -9 2>/dev/null || true
fi
pids=$(pgrep -f "cocoon.*-c " 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo "$pids" | xargs kill -9 2>/dev/null || true
fi
sleep 0.5
# 确保端口已释放
for i in {1..20}; do