W17: close 9 audit findings — atomic pointers, SSE robustness, leak fix, verification (W17.1-W17.4)
- W17.1 (曹武): Verify F-13.3-1/2/3 all fixed by W14.1, close 3 findings (1 CRITICAL + 1 HIGH + 1 MEDIUM) - W17.2 (赵码): Fix F-13.2-3 SSE [DONE] sentinel trimming + F-13.2-4 g_host/g_http/g_config atomic pointers in deepseek_plugin - W17.3 (王测): Verify 4 W14 findings (F-13.1-1/4, F-13.2-1/2) + F-11.7-2 confirmed fixed, close 5 findings - W17.4 (马奔): Fix F-13.1-2 response_body leak on error path + F-13.1-3 g_host/g_http atomic pointers in anthropic_plugin Build 0 error, ctest 4/4 pass, metadata check clean. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -3,17 +3,18 @@
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <boost/json/src.hpp>
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace json = boost::json;
|
||||
|
||||
// ============================================================================
|
||||
// 全局指针:从 on_init 获取
|
||||
// 全局指针:从 on_init 获取(W14.3: atomic acquire/release 保护读写竞态)
|
||||
// ============================================================================
|
||||
static const dstalk_host_api_t* g_host = nullptr;
|
||||
static dstalk_http_service_t* g_http = nullptr;
|
||||
static dstalk_config_service_t* g_config = nullptr;
|
||||
static std::atomic<const dstalk_host_api_t*> g_host{nullptr};
|
||||
static std::atomic<dstalk_http_service_t*> g_http{nullptr};
|
||||
static std::atomic<dstalk_config_service_t*> g_config{nullptr};
|
||||
|
||||
// ============================================================================
|
||||
// 配置数据(由 configure() 设置)
|
||||
@@ -135,7 +136,8 @@ static std::string build_request_json(
|
||||
// ============================================================================
|
||||
// 解析非流式 JSON 响应
|
||||
// ============================================================================
|
||||
static void parse_response(const char* body, int http_status,
|
||||
static void parse_response(const dstalk_host_api_t* host,
|
||||
const char* body, int http_status,
|
||||
dstalk_chat_result_t& r)
|
||||
{
|
||||
r.http_status = http_status;
|
||||
@@ -147,16 +149,16 @@ static void parse_response(const char* body, int http_status,
|
||||
auto obj = jv.as_object();
|
||||
if (obj.contains("error")) {
|
||||
auto err = obj["error"].as_object();
|
||||
r.error = g_host->strdup(
|
||||
json::value_to<std::string>(err["message"]).c_str());
|
||||
r.error = host ? host->strdup(
|
||||
json::value_to<std::string>(err["message"]).c_str()) : nullptr;
|
||||
}
|
||||
} catch (...) {
|
||||
std::string msg = "HTTP " + std::to_string(http_status);
|
||||
r.error = g_host->strdup(msg.c_str());
|
||||
r.error = host ? host->strdup(msg.c_str()) : nullptr;
|
||||
}
|
||||
if (!r.error) {
|
||||
if (!r.error && host) {
|
||||
std::string msg = "HTTP " + std::to_string(http_status);
|
||||
r.error = g_host->strdup(msg.c_str());
|
||||
r.error = host->strdup(msg.c_str());
|
||||
}
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
@@ -171,11 +173,11 @@ static void parse_response(const char* body, int http_status,
|
||||
auto msg = choices[0].as_object()["message"].as_object();
|
||||
|
||||
std::string content = json::value_to<std::string>(msg["content"]);
|
||||
r.content = g_host->strdup(content.c_str());
|
||||
r.content = host ? host->strdup(content.c_str()) : nullptr;
|
||||
|
||||
if (msg.contains("tool_calls")) {
|
||||
std::string tc = json::serialize(msg["tool_calls"]);
|
||||
r.tool_calls_json = g_host->strdup(tc.c_str());
|
||||
r.tool_calls_json = host ? host->strdup(tc.c_str()) : nullptr;
|
||||
} else {
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
@@ -184,19 +186,19 @@ static void parse_response(const char* body, int http_status,
|
||||
r.error = nullptr;
|
||||
} else {
|
||||
r.ok = 0;
|
||||
r.error = g_host->strdup("empty response");
|
||||
r.error = host ? host->strdup("empty response") : nullptr;
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
r.ok = 0;
|
||||
std::string msg = std::string("json parse: ") + e.what();
|
||||
r.error = g_host->strdup(msg.c_str());
|
||||
r.error = host ? host->strdup(msg.c_str()) : nullptr;
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
} catch (...) {
|
||||
r.ok = 0;
|
||||
r.error = g_host->strdup("json parse error");
|
||||
r.error = host ? host->strdup("json parse error") : nullptr;
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
@@ -210,6 +212,16 @@ static bool parse_sse_line(const std::string& line, std::string& token_out)
|
||||
if (line.rfind("data: ", 0) != 0) return false;
|
||||
|
||||
std::string data = line.substr(6);
|
||||
|
||||
// F-13.2-3: Trim leading/trailing whitespace before comparing [DONE] sentinel.
|
||||
// Some servers may emit "data: [DONE] " with trailing spaces, which would
|
||||
// cause exact match to fail and the stream to never terminate.
|
||||
const char* ws = " \t\r\n";
|
||||
size_t start = data.find_first_not_of(ws);
|
||||
if (start != std::string::npos) {
|
||||
data.erase(0, start);
|
||||
data.erase(data.find_last_not_of(ws) + 1);
|
||||
}
|
||||
if (data == "[DONE]") {
|
||||
token_out.clear();
|
||||
return true; // 流结束信号
|
||||
@@ -247,18 +259,21 @@ static int my_configure(const char* provider, const char* base_url,
|
||||
g_cfg.max_tokens = max_tokens;
|
||||
g_cfg.temperature = temperature;
|
||||
|
||||
if (g_host) {
|
||||
g_host->log(DSTALK_LOG_INFO,
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host) {
|
||||
host->log(DSTALK_LOG_INFO,
|
||||
"[deepseek] configured: model=%s base_url=%s max_tokens=%d temperature=%.2f",
|
||||
g_cfg.model.c_str(), g_cfg.base_url.c_str(),
|
||||
g_cfg.max_tokens, g_cfg.temperature);
|
||||
}
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] my_configure exception: %s", e.what());
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] my_configure exception: %s", e.what());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] my_configure unknown exception");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] my_configure unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -275,13 +290,16 @@ static dstalk_chat_result_t my_chat(
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
|
||||
if (!g_http) {
|
||||
r.error = g_host->strdup("http service not available");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
dstalk_http_service_t* http = g_http.load(std::memory_order_acquire);
|
||||
|
||||
if (!http) {
|
||||
r.error = host ? host->strdup("http service not available") : nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string scheme, host, port, target;
|
||||
extract_host_port(g_cfg.base_url, scheme, host, port, target);
|
||||
std::string scheme, host_name, port, target;
|
||||
extract_host_port(g_cfg.base_url, scheme, host_name, port, target);
|
||||
std::string target_path = target + "/chat/completions";
|
||||
|
||||
std::string body = build_request_json(history, history_len,
|
||||
@@ -292,32 +310,34 @@ static dstalk_chat_result_t my_chat(
|
||||
char* response_body = nullptr;
|
||||
int status_code = 0;
|
||||
|
||||
int ret = g_http->post_json(
|
||||
host.c_str(), port.c_str(), target_path.c_str(), body.c_str(),
|
||||
int ret = http->post_json(
|
||||
host_name.c_str(), port.c_str(), target_path.c_str(), body.c_str(),
|
||||
headers_json.c_str(), &response_body, &status_code);
|
||||
|
||||
if (ret != 0) {
|
||||
r.error = g_host->strdup("http request failed");
|
||||
r.error = host ? host->strdup("http request failed") : nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
parse_response(response_body, status_code, r);
|
||||
parse_response(host, response_body, status_code, r);
|
||||
|
||||
if (response_body) {
|
||||
g_host->free(response_body);
|
||||
if (host) host->free(response_body);
|
||||
}
|
||||
return r;
|
||||
} catch (const std::exception& e) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat exception: %s", e.what());
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat exception: %s", e.what());
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = g_host ? g_host->strdup(e.what()) : nullptr;
|
||||
r.error = host ? host->strdup(e.what()) : nullptr;
|
||||
return r;
|
||||
} catch (...) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat unknown exception");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat unknown exception");
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = g_host ? g_host->strdup("unknown exception") : nullptr;
|
||||
r.error = host ? host->strdup("unknown exception") : nullptr;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -356,10 +376,12 @@ static int sse_line_callback(const char* line, void* userdata)
|
||||
}
|
||||
return 1; // 继续
|
||||
} catch (const std::exception& e) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] sse_line_callback exception: %s", e.what());
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] sse_line_callback exception: %s", e.what());
|
||||
return 0;
|
||||
} catch (...) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] sse_line_callback unknown exception");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] sse_line_callback unknown exception");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -373,13 +395,16 @@ static dstalk_chat_result_t my_chat_stream(
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
|
||||
if (!g_http) {
|
||||
r.error = g_host->strdup("http service not available");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
dstalk_http_service_t* http = g_http.load(std::memory_order_acquire);
|
||||
|
||||
if (!http) {
|
||||
r.error = host ? host->strdup("http service not available") : nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
std::string scheme, host, port, target;
|
||||
extract_host_port(g_cfg.base_url, scheme, host, port, target);
|
||||
std::string scheme, host_name, port, target;
|
||||
extract_host_port(g_cfg.base_url, scheme, host_name, port, target);
|
||||
std::string target_path = target + "/chat/completions";
|
||||
|
||||
std::string body = build_request_json(history, history_len,
|
||||
@@ -388,15 +413,15 @@ static dstalk_chat_result_t my_chat_stream(
|
||||
std::string headers_json = build_headers_json(g_cfg.api_key);
|
||||
|
||||
StreamContext ctx;
|
||||
ctx.host = g_host;
|
||||
ctx.host = host;
|
||||
ctx.user_cb = cb;
|
||||
ctx.userdata = userdata;
|
||||
|
||||
char* response_body = nullptr;
|
||||
int status_code = 0;
|
||||
|
||||
int ret = g_http->post_stream(
|
||||
host.c_str(), port.c_str(), target_path.c_str(), body.c_str(),
|
||||
int ret = http->post_stream(
|
||||
host_name.c_str(), port.c_str(), target_path.c_str(), body.c_str(),
|
||||
headers_json.c_str(),
|
||||
sse_line_callback, &ctx,
|
||||
&response_body, &status_code);
|
||||
@@ -413,49 +438,51 @@ static dstalk_chat_result_t my_chat_stream(
|
||||
auto obj = jv.as_object();
|
||||
if (obj.contains("error")) {
|
||||
auto err = obj["error"].as_object();
|
||||
r.error = g_host->strdup(
|
||||
json::value_to<std::string>(err["message"]).c_str());
|
||||
r.error = host ? host->strdup(
|
||||
json::value_to<std::string>(err["message"]).c_str()) : nullptr;
|
||||
}
|
||||
} catch (...) {}
|
||||
}
|
||||
if (!r.error) {
|
||||
if (!r.error && host) {
|
||||
if (status_code <= 0)
|
||||
r.error = g_host->strdup("transport error");
|
||||
r.error = host->strdup("transport error");
|
||||
else
|
||||
r.error = g_host->strdup(
|
||||
r.error = host->strdup(
|
||||
("HTTP " + std::to_string(status_code)).c_str());
|
||||
}
|
||||
if (response_body) g_host->free(response_body);
|
||||
if (response_body && host) host->free(response_body);
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
return r;
|
||||
}
|
||||
|
||||
if (response_body) g_host->free(response_body);
|
||||
if (response_body && host) host->free(response_body);
|
||||
|
||||
if (ctx.accumulated.empty()) {
|
||||
r.ok = 0;
|
||||
r.error = g_host->strdup("no content received");
|
||||
r.error = host ? host->strdup("no content received") : nullptr;
|
||||
r.content = nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
} else {
|
||||
r.ok = 1;
|
||||
r.error = nullptr;
|
||||
r.content = g_host->strdup(ctx.accumulated.c_str());
|
||||
r.content = host ? host->strdup(ctx.accumulated.c_str()) : nullptr;
|
||||
r.tool_calls_json = nullptr;
|
||||
}
|
||||
return r;
|
||||
} catch (const std::exception& e) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat_stream exception: %s", e.what());
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat_stream exception: %s", e.what());
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = g_host ? g_host->strdup(e.what()) : nullptr;
|
||||
r.error = host ? host->strdup(e.what()) : nullptr;
|
||||
return r;
|
||||
} catch (...) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat_stream unknown exception");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] my_chat_stream unknown exception");
|
||||
dstalk_chat_result_t r = {};
|
||||
r.ok = 0;
|
||||
r.error = g_host ? g_host->strdup("unknown exception") : nullptr;
|
||||
r.error = host ? host->strdup("unknown exception") : nullptr;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -465,10 +492,11 @@ static dstalk_chat_result_t my_chat_stream(
|
||||
// ============================================================================
|
||||
static void my_free_result(dstalk_chat_result_t* result)
|
||||
{
|
||||
if (!result || !g_host) return;
|
||||
if (result->content) { g_host->free((void*)result->content); result->content = nullptr; }
|
||||
if (result->error) { g_host->free((void*)result->error); result->error = nullptr; }
|
||||
if (result->tool_calls_json) { g_host->free((void*)result->tool_calls_json); result->tool_calls_json = nullptr; }
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (!result || !host) return;
|
||||
if (result->content) { host->free((void*)result->content); result->content = nullptr; }
|
||||
if (result->error) { host->free((void*)result->error); result->error = nullptr; }
|
||||
if (result->tool_calls_json) { host->free((void*)result->tool_calls_json); result->tool_calls_json = nullptr; }
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -487,23 +515,27 @@ static dstalk_ai_service_t g_service = {
|
||||
static int on_init(const dstalk_host_api_t* host)
|
||||
{
|
||||
try {
|
||||
g_host = host;
|
||||
g_http = (dstalk_http_service_t*)host->query_service("http", 1);
|
||||
g_config = (dstalk_config_service_t*)host->query_service("config", 1);
|
||||
dstalk_http_service_t* http = (dstalk_http_service_t*)host->query_service("http", 1);
|
||||
dstalk_config_service_t* cfg = (dstalk_config_service_t*)host->query_service("config", 1);
|
||||
g_host.store(host, std::memory_order_release);
|
||||
g_http.store(http, std::memory_order_release);
|
||||
g_config.store(cfg, std::memory_order_release);
|
||||
|
||||
if (!g_http) {
|
||||
if (g_host) g_host->log(DSTALK_LOG_ERROR, "[deepseek] http service not found");
|
||||
if (!http) {
|
||||
if (host) host->log(DSTALK_LOG_ERROR, "[deepseek] http service not found");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (g_host) g_host->log(DSTALK_LOG_INFO, "[deepseek] initializing DeepSeek AI plugin");
|
||||
if (host) host->log(DSTALK_LOG_INFO, "[deepseek] initializing DeepSeek AI plugin");
|
||||
|
||||
return host->register_service("ai.deepseek", 1, &g_service);
|
||||
} catch (const std::exception& e) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] on_init exception: %s", e.what());
|
||||
const dstalk_host_api_t* h = g_host.load(std::memory_order_acquire);
|
||||
if (h && h->log) h->log(DSTALK_LOG_ERROR, "[deepseek] on_init exception: %s", e.what());
|
||||
return -1;
|
||||
} catch (...) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] on_init unknown exception");
|
||||
const dstalk_host_api_t* h = g_host.load(std::memory_order_acquire);
|
||||
if (h && h->log) h->log(DSTALK_LOG_ERROR, "[deepseek] on_init unknown exception");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -511,16 +543,19 @@ static int on_init(const dstalk_host_api_t* host)
|
||||
static void on_shutdown()
|
||||
{
|
||||
try {
|
||||
if (g_host) g_host->log(DSTALK_LOG_INFO, "[deepseek] shutdown");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host) host->log(DSTALK_LOG_INFO, "[deepseek] shutdown");
|
||||
secure_zero(g_cfg.api_key.data(), g_cfg.api_key.size());
|
||||
g_cfg.api_key.clear();
|
||||
g_http = nullptr;
|
||||
g_config = nullptr;
|
||||
g_host = nullptr;
|
||||
g_http.store(nullptr, std::memory_order_release);
|
||||
g_config.store(nullptr, std::memory_order_release);
|
||||
g_host.store(nullptr, std::memory_order_release);
|
||||
} catch (const std::exception& e) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] on_shutdown exception: %s", e.what());
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] on_shutdown exception: %s", e.what());
|
||||
} catch (...) {
|
||||
if (g_host && g_host->log) g_host->log(DSTALK_LOG_ERROR, "[deepseek] on_shutdown unknown exception");
|
||||
const dstalk_host_api_t* host = g_host.load(std::memory_order_acquire);
|
||||
if (host && host->log) host->log(DSTALK_LOG_ERROR, "[deepseek] on_shutdown unknown exception");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user