Skip to content

Commit 785c399

Browse files
authored
Use ZIP file size metadata to allocate string (#13032)
1 parent a35cdbb commit 785c399

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/ruff_db/src/vendored.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,16 @@ impl VendoredFileSystem {
9797
fn read_to_string(fs: &VendoredFileSystem, path: &VendoredPath) -> Result<String> {
9898
let mut archive = fs.lock_archive();
9999
let mut zip_file = archive.lookup_path(&NormalizedVendoredPath::from(path))?;
100-
let mut buffer = String::new();
100+
101+
// Pre-allocate the buffer with the size specified in the ZIP file metadata
102+
// because `read_to_string` passes `None` as the size hint.
103+
// But let's not trust the zip file metadata (even though it's vendored)
104+
// and limit it to a reasonable size.
105+
let mut buffer = String::with_capacity(
106+
usize::try_from(zip_file.size())
107+
.unwrap_or(usize::MAX)
108+
.min(10_000_000),
109+
);
101110
zip_file.read_to_string(&mut buffer)?;
102111
Ok(buffer)
103112
}

0 commit comments

Comments
 (0)