Skip to content

Commit aa5269d

Browse files
SummerOneTwoclaude
andcommitted
fix: 修复 Windows 平台 testlib 程序兼容性问题
- 添加静态链接 (-static) 避免 DLL 版本冲突导致的崩溃 - 添加 LF 到 CRLF 转换满足 testlib readEoln() 在 Windows 上的要求 - 更新 generator 模板添加防止死循环的安全检查示例 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 5dfa195 commit aa5269d

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/autocode_mcp/templates/generator_template.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Generator 模板 - 基于 testlib.h
22
// 用于生成测试数据
3+
//
4+
// 安全提示:
5+
// 1. 使用 do-while 循环生成不重复元素时,务必添加 attempts 计数器防止死循环
6+
// 2. 确保参数范围足够大以生成所需数量的不重复元素
7+
// 3. 示例:生成不重复坐标时,确保 L >= N + 1
38

49
#include "testlib.h"
510
#include <iostream>
@@ -41,6 +46,11 @@ int main(int argc, char* argv[]) {
4146
// 输出测试数据
4247
std::cout << n << std::endl;
4348

49+
// 示例:生成不重复元素的数组(带安全检查)
50+
// 确保 n 不超过值域范围
51+
// int value_range = 1000000000;
52+
// if (n > value_range) n = value_range; // 防止无法生成不重复元素
53+
4454
for (int i = 0; i < n; i++) {
4555
if (i > 0) std::cout << " ";
4656
std::cout << rnd.next(1, 1000000000);
@@ -49,3 +59,32 @@ int main(int argc, char* argv[]) {
4959

5060
return 0;
5161
}
62+
63+
// === 生成不重复坐标的安全示例 ===
64+
// 当需要生成 N 个不重复的坐标 (x, y) 时:
65+
//
66+
// void generateUniqueCoords(int n, long long L) {
67+
// // 安全检查:确保 L 足够大
68+
// L = std::max(L, (long long)(n + 1));
69+
//
70+
// std::set<std::pair<long long, long long>> used;
71+
// int attempts = 0;
72+
// const int MAX_ATTEMPTS = n * 100; // 防止死循环
73+
//
74+
// while (used.size() < n && attempts < MAX_ATTEMPTS) {
75+
// long long x = rnd.next(0LL, L - 1);
76+
// long long y = rnd.next(0LL, L - 1);
77+
// auto coord = std::make_pair(x, y);
78+
//
79+
// if (used.find(coord) == used.end()) {
80+
// used.insert(coord);
81+
// std::cout << x << " " << y << std::endl;
82+
// }
83+
// attempts++;
84+
// }
85+
//
86+
// // 如果无法生成足够的不重复坐标,输出警告或调整参数
87+
// if (used.size() < n) {
88+
// // 可以选择输出剩余坐标或抛出错误
89+
// }
90+
// }

src/autocode_mcp/utils/compiler.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ async def compile_cpp(
150150
binary_path,
151151
]
152152

153+
# Windows 上使用静态链接避免 DLL 版本冲突(特别是 testlib.h 程序)
154+
if sys.platform == "win32":
155+
cmd.append("-static")
156+
153157
try:
154158
process = await asyncio.create_subprocess_exec(
155159
*cmd,
@@ -316,8 +320,16 @@ async def _run_process(
316320
)
317321

318322
try:
323+
# Windows 上 testlib strict 模式期望 CRLF 换行符
324+
# 将 LF 转换为 CRLF 以满足 validator 的 readEoln() 要求
325+
processed_stdin = stdin
326+
if sys.platform == "win32" and stdin:
327+
# 避免重复转换:先还原已有的 CRLF,再将所有 LF 转为 CRLF
328+
processed_stdin = stdin.replace("\r\n", "\n").replace("\n", "\r\n")
329+
319330
stdout, stderr = await asyncio.wait_for(
320-
process.communicate(input=stdin.encode("utf-8") if stdin else None), timeout=timeout
331+
process.communicate(input=processed_stdin.encode("utf-8") if processed_stdin else None),
332+
timeout=timeout,
321333
)
322334
except TimeoutError:
323335
# 超时时强制终止进程

0 commit comments

Comments
 (0)