Treat buffer size as usize

`usize` is made exactly for this (e.g.: the size of a buffer), and
signatures are a bit more idiomatic.

These changes also try to cast the u32 to a usize as soon as we read it,
and will panic if there is an issue (whereas using `as usize` may result
in silent conversion errors on uncommon architectures.
This commit is contained in:
Hugo Osvaldo Barrera
2022-06-01 18:42:33 +02:00
parent bd10966096
commit dda48b6f84
2 changed files with 13 additions and 12 deletions
+9 -8
View File
@@ -3,6 +3,7 @@ extern crate nix;
#[cfg(windows)]
extern crate named_pipe;
use std::convert::TryInto;
use std::io::{self, Read, Write};
use std::thread;
use byteorder::{ByteOrder, NativeEndian, WriteBytesExt};
@@ -11,23 +12,23 @@ mod proxy_socket;
use proxy_socket::ProxySocket;
const BUFFER_SIZE: u32 = 1024 ^ 2; // 1024 ^ 2 is the maximum
const BUFFER_SIZE: usize = 1024 ^ 2; // 1024 ^ 2 is the maximum
fn valid_length(length: u32) -> bool {
return length > 0 && length <= BUFFER_SIZE;
fn valid_length(length: usize) -> bool {
length > 0 && length <= BUFFER_SIZE
}
fn read_header() -> u32 {
fn read_header() -> usize {
let stdin = io::stdin();
let mut buf = vec![0; 4];
let mut handle = stdin.lock();
handle.read_exact(&mut buf).unwrap();
NativeEndian::read_u32(&buf)
NativeEndian::read_u32(&buf).try_into().unwrap()
}
fn read_body<T: Read + Write>(length: u32, socket: &mut ProxySocket<T>) {
let mut buffer = vec![0; length as usize];
fn read_body<T: Read + Write>(length: usize, socket: &mut ProxySocket<T>) {
let mut buffer = vec![0; length];
let stdin = io::stdin();
let mut handle = stdin.lock();
@@ -39,7 +40,7 @@ fn read_body<T: Read + Write>(length: u32, socket: &mut ProxySocket<T>) {
}
fn read_response<T: Read>(socket: &mut ProxySocket<T>) {
let mut buf = vec![0; BUFFER_SIZE as usize];
let mut buf = vec![0; BUFFER_SIZE];
if let Ok(len) = socket.read(&mut buf) {
write_response(&buf[0..len]);
}
+4 -4
View File
@@ -32,7 +32,7 @@ impl<W: Write> Write for ProxySocket<W> {
}
#[cfg(windows)]
pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<PipeClient>> {
pub fn connect(buffer_size: usize) -> io::Result<ProxySocket<PipeClient>> {
let username = env::var("USERNAME").unwrap();
let pipe_name = format!("\\\\.\\pipe\\keepassxc\\{}\\org.keepassxc.KeePassXC.BrowserServer", username);
let client = PipeClient::connect(pipe_name)?;
@@ -40,7 +40,7 @@ pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<PipeClient>> {
}
#[cfg(not(windows))]
pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<UnixStream>> {
pub fn connect(buffer_size: usize) -> io::Result<ProxySocket<UnixStream>> {
use std::time::Duration;
let socket_name = "org.keepassxc.KeePassXC.BrowserServer";
@@ -50,8 +50,8 @@ pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<UnixStream>> {
format!("/tmp/{}", socket_name)
};
let s = UnixStream::connect(socket)?;
socket::setsockopt(s.as_raw_fd(), SndBuf, &(buffer_size as usize)).expect("setsockopt for SndBuf failed");
socket::setsockopt(s.as_raw_fd(), RcvBuf, &(buffer_size as usize)).expect("setsockopt for RcvBuf failed");
socket::setsockopt(s.as_raw_fd(), SndBuf, &buffer_size).expect("setsockopt for SndBuf failed");
socket::setsockopt(s.as_raw_fd(), RcvBuf, &buffer_size).expect("setsockopt for RcvBuf failed");
let timeout: Option<Duration> = Some(Duration::from_secs(1));
s.set_read_timeout(timeout)?;
Ok(ProxySocket { inner: s })