Fix overflowing subtract in vec preallocation
authorMatt Corallo <git@bluematt.me>
Sun, 11 Feb 2024 20:33:35 +0000 (20:33 +0000)
committerMatt Corallo <git@bluematt.me>
Sun, 11 Feb 2024 20:33:35 +0000 (20:33 +0000)
If a TXT record comes in with zero length data, we'll overflow in
the subtraction and try to allocate much too much data.

src/rr.rs

index 9886e3a71ceff967da990ffc8fa882d806650241..c5855559c9efa1479e44d2568a2b152092f6c885 100644 (file)
--- a/src/rr.rs
+++ b/src/rr.rs
@@ -218,7 +218,7 @@ impl StaticRecord for Txt {
                format!("{{\"type\":\"txt\",\"name\":\"{}\",\"contents\":{:?}}}", self.name.0, &self.data[..])
        }
        fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
-               let mut parsed_data = Vec::with_capacity(data.len() - 1);
+               let mut parsed_data = Vec::with_capacity(data.len().saturating_sub(1));
                while !data.is_empty() {
                        let len = read_u8(&mut data)? as usize;
                        if data.len() < len { return Err(()); }