Skip to content

Commit 6d7a59a

Browse files
committed
fix(build): bound test discovery report reads
1 parent 6f005ab commit 6d7a59a

2 files changed

Lines changed: 50 additions & 10 deletions

File tree

‎rust/crates/build/src/decode.rs‎

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use lithe_plugin_model::{PluginRejection, Validation};
1010
use lithe_workspace::{self as workspace, fs};
1111
use serde_json::Value;
12+
use std::io::Read;
1213
use std::path::{Component, Path, PathBuf};
1314
use std::time::UNIX_EPOCH;
1415

@@ -62,24 +63,41 @@ fn first_line(text: &str) -> &str {
6263

6364
/// Reads a UTF-8 file up to the output bound.
6465
pub(crate) fn read_bounded(path: &Path) -> Validation<String> {
66+
String::from_utf8(read_file_bytes(path)?)
67+
.map_err(|_| invalid_output("The build tool output is not UTF-8."))
68+
}
69+
70+
/// Bytes of a file up to the output bound, or `None` when it is absent, not
71+
/// a regular file, or too large.
72+
pub(crate) fn read_bytes_bounded(path: &Path) -> Option<Vec<u8>> {
73+
// Sync fingerprints treat an unreadable input as absent.
74+
read_file_bytes(path).ok()
75+
}
76+
77+
fn read_file_bytes(path: &Path) -> Validation<Vec<u8>> {
6578
let metadata = fs::metadata(path)
6679
.map_err(|_| invalid_output("The build tool did not write its output file."))?;
6780
if !metadata.is_file() || metadata.len() > MAX_OUTPUT_BYTES as u64 {
6881
return Err(invalid_output(
6982
"The build tool output file is missing or too large.",
7083
));
7184
}
72-
fs::read_to_string(path).map_err(|_| invalid_output("The build tool output is not UTF-8."))
85+
let file = fs::File::open(path)
86+
.map_err(|_| invalid_output("The build tool output file could not be opened."))?;
87+
read_output(file, metadata.len() as usize)
7388
}
7489

75-
/// Bytes of a file up to the output bound, or `None` when it is absent, not
76-
/// a regular file, or too large.
77-
pub(crate) fn read_bytes_bounded(path: &Path) -> Option<Vec<u8>> {
78-
let metadata = fs::metadata(path).ok()?;
79-
if !metadata.is_file() || metadata.len() > MAX_OUTPUT_BYTES as u64 {
80-
return None;
90+
fn read_output(reader: impl Read, observed_size: usize) -> Validation<Vec<u8>> {
91+
let mut bytes = Vec::with_capacity(observed_size + 1);
92+
// The opened file may have grown since the metadata check.
93+
reader
94+
.take(MAX_OUTPUT_BYTES as u64 + 1)
95+
.read_to_end(&mut bytes)
96+
.map_err(|_| invalid_output("The build tool output file could not be read."))?;
97+
if bytes.len() > MAX_OUTPUT_BYTES {
98+
return Err(invalid_output("The build tool output is too large."));
8199
}
82-
fs::read(path).ok()
100+
Ok(bytes)
83101
}
84102

85103
/// Bounds a host-provided output text before it is parsed line by line.
@@ -428,6 +446,29 @@ fn is_current(path: &Path, not_before_millis: Option<u64>) -> bool {
428446
mod tests {
429447
use super::*;
430448

449+
#[test]
450+
fn output_read_enforces_the_limit_even_if_the_file_grows() {
451+
let limit = MAX_OUTPUT_BYTES as u64;
452+
let at_limit = std::io::repeat(b'x').take(limit);
453+
assert_eq!(
454+
read_output(at_limit, MAX_OUTPUT_BYTES).unwrap().len(),
455+
MAX_OUTPUT_BYTES
456+
);
457+
458+
// Metadata reported an in-budget size, but the stream contains more.
459+
let mut over_limit = std::io::repeat(b'x').take(limit + 2);
460+
let rejected = match read_output(&mut over_limit, MAX_OUTPUT_BYTES) {
461+
Ok(bytes) => panic!("accepted an oversized output of {} bytes", bytes.len()),
462+
Err(error) => error,
463+
};
464+
assert_eq!(rejected.code, "plugin.build.invalidOutput");
465+
assert_eq!(
466+
over_limit.limit(),
467+
1,
468+
"read past the extra byte needed to detect overflow"
469+
);
470+
}
471+
431472
#[test]
432473
fn wildcards_match_within_a_segment() {
433474
assert!(wildcard("TEST-*.xml", "TEST-demo.OrderTest.xml"));

‎shared/plugins/reference/protocol-changelog.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
- [#112](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/112),[@Mucheen](https://github.com/Mucheen),**`buildSystems` 1**(API-29):清单新增 `buildSystems[].test.discoveryCommand`,已有测试运行可以独立采用命令或 provider。命令使用现有构建工具,把 `litheTestDiscovery` v0 写到宿主提供的 `${outputFile}`。Core 的 `build.testDiscoveryPlan`、`build.decodeTestDiscovery` 和测试树兼容两种发现来源,检查每条选择器可原样运行,命令尝试沿用 `lithe/build/discoverTests` 的结构化终态。没有新增宿主方法、通知或 SDK 版本。见 [1lck/Lithe-IDEA#2122](https://github.com/1lck/Lithe-IDEA/issues/2122)。
1818

19-
目前没有。
20-
2119
## 按协议查首次出现
2220

2321
所有版本都从 `0` 开始。以后一个 PR 给某个能力加版本 `1` 时,在对应的表里为它加一行。
@@ -47,6 +45,7 @@
4745
| `buildProviders` | 0 | 2026-11-11 | [#17](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/17) |
4846
| `buildProviders` | 1 | 2026-11-18 | [#89](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/89) |
4947
| `buildSystems` | 0 | 2026-11-11 | [#17](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/17) |
48+
| `buildSystems` | 1 | 待合入 | [#112](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/112) |
5049
| `commandInstallers` | 0 | 2026-11-15 | [#53](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/53) |
5150
| `debugAdapters` | 0 | 2026-11-11 | [#17](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/17) |
5251
| `executionProviders` | 0 | 2026-11-11 | [#17](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/17) |

0 commit comments

Comments
 (0)