Skip to content

Commit aa06096

Browse files
committed
fix: handle integer overflows
Passing huge values might lead to integer overflows during calculations. Signed-off-by: Tobias Stoeckmann <[email protected]>
1 parent 61a813b commit aa06096

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/spooled.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl Write for SpooledTempFile {
157157
// roll over to file if necessary
158158
if matches! {
159159
&self.inner, SpooledData::InMemory(cursor)
160-
if cursor.position() as usize + buf.len() > self.max_size
160+
if cursor.position().saturating_add(buf.len() as u64) > self.max_size as u64
161161
} {
162162
self.roll()?;
163163
}
@@ -173,8 +173,10 @@ impl Write for SpooledTempFile {
173173
if matches! {
174174
&self.inner, SpooledData::InMemory(cursor)
175175
// Borrowed from the rust standard library.
176-
if cursor.position() as usize + bufs.iter()
177-
.fold(0usize, |a, b| a.saturating_add(b.len())) > self.max_size
176+
if bufs
177+
.iter()
178+
.fold(cursor.position(), |a, b| a.saturating_add(b.len() as u64))
179+
> self.max_size as u64
178180
} {
179181
self.roll()?;
180182
}

src/util.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use std::{io, iter::repeat_with};
55
use crate::error::IoResultExt;
66

77
fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString {
8-
let mut buf = OsString::with_capacity(prefix.len() + suffix.len() + rand_len);
8+
let capacity = prefix
9+
.len()
10+
.saturating_add(suffix.len())
11+
.saturating_add(rand_len);
12+
let mut buf = OsString::with_capacity(capacity);
913
buf.push(prefix);
1014
let mut char_buf = [0u8; 4];
1115
for c in repeat_with(fastrand::alphanumeric).take(rand_len) {

tests/spooled.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ fn test_set_len_rollover() {
306306
assert_eq!(buf.as_slice(), b"abcde\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
307307
}
308308

309+
#[test]
310+
fn test_write_overflow() {
311+
let mut t = spooled_tempfile(10);
312+
t.seek(SeekFrom::Start(u64::MAX)).unwrap();
313+
assert!(t.write(b"abcde").is_err());
314+
}
315+
309316
#[cfg(target_pointer_width = "32")]
310317
#[test]
311318
fn test_set_len_truncation() {

0 commit comments

Comments
 (0)