From 8dc05f534682100e7211a8dde7bec70d848981d1 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Fri, 6 Nov 2020 23:04:21 +0100 Subject: [PATCH] Erasing arguments with Vec. --- src/packet.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/packet.rs b/src/packet.rs index 16536f2..cc625a2 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -5,6 +5,7 @@ use crate::constants::SSH_FXP_INIT; use crate::constants::SSH_FXP_VERSION; use std::io::{BufReader, BufWriter, Read, Write}; +use std::process::exit; pub fn dispatch_packet(packet: Packet) { let mut buff_stdout = BufWriter::new(std::io::stdout()); @@ -31,7 +32,9 @@ pub fn dispatch_packet(packet: Packet) { }; let serialized_response = response_packet.serialize(); eprintln!("{:#?}", &serialized_response); - buff_stdout.write(&serialized_response).expect("Unable to write to stdout."); + buff_stdout + .write(&serialized_response) + .expect("Unable to write to stdout."); buff_stdout.flush().expect("Unable to flush stdout."); } } @@ -56,18 +59,26 @@ impl Packet { packet_header.length, packet_header.type_packet ); let mut data: Vec = Vec::new(); - data.resize(packet_header.length as usize - 1, 0); + if packet_header.length == 0 { + exit(0); + } + if packet_header.length as usize - 1 > 34000 { + panic!( + "This packet is bigger than supported {}, refusing to allocate memory to handle that.", packet_header.length + ); + } + data.resize(packet_header.length as usize - 1, 10); match buff_stdin.read(&mut data) { Ok(ok) => ok, Err(err) => panic!("Could not fetch data: {}", err), }; eprintln!("{:#?}", data); - Packet::deserialize(packet_header, data) + Packet::deserialize(packet_header, &data) } - pub fn deserialize(packet_header: header::PacketHeader, data: Vec) -> Packet { + pub fn deserialize(packet_header: header::PacketHeader, data: &[u8]) -> Packet { Packet { packet_header: packet_header, - data: data, + data: data.to_vec(), } } pub fn serialize(&mut self) -> Vec {