summaryrefslogtreecommitdiff
path: root/crates/parser/src/lib.rs
diff options
context:
space:
mode:
authorHenry <mail@henrygressmann.de>2026-09-16 12:06:46 +0200
committerHenry <mail@henrygressmann.de>2026-09-16 12:06:46 +0200
commit6a7a6af9e25d0daa49bb142c85a298c5c81e76a4 (patch)
tree347addd3ce4b8b9b4914b0fc562087d92ca426b1 /crates/parser/src/lib.rs
parentdc8e5732d71b6f66e730b090ab02bdccc69ec888 (diff)
fix: harden instantiation, stream parsing, and host reentrancy
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/parser/src/lib.rs')
-rw-r--r--crates/parser/src/lib.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index 2752d8b..019e027 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -190,10 +190,24 @@ impl Parser {
#[cfg(feature = "std")]
fn read_more(stream: &mut impl std::io::Read, buffer: &mut alloc::vec::Vec<u8>, hint: usize) -> Result<usize> {
let len = buffer.len();
- buffer.resize(len + hint, 0);
- let read_bytes = stream
- .read(&mut buffer[len..])
- .map_err(|e| ParseError::Other(alloc::format!("Error reading from stream: {e}")))?;
+ // Size hints can come from untrusted section lengths.
+ let increment = hint.clamp(1, 64 * 1024);
+ let new_len =
+ len.checked_add(increment).ok_or_else(|| ParseError::Other("stream buffer is too large".into()))?;
+ buffer
+ .try_reserve(increment)
+ .map_err(|e| ParseError::Other(alloc::format!("Error reserving stream buffer: {e}")))?;
+ buffer.resize(new_len, 0);
+ let read_bytes = loop {
+ match stream.read(&mut buffer[len..]) {
+ Ok(read_bytes) => break read_bytes,
+ Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
+ Err(e) => {
+ buffer.truncate(len);
+ return Err(ParseError::Other(alloc::format!("Error reading from stream: {e}")));
+ }
+ }
+ };
buffer.truncate(len + read_bytes);
Ok(read_bytes)
}