Skip to content

Commit 2509437

Browse files
committed
Fix excessive stack sizes
1 parent 574769f commit 2509437

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/lib.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,9 @@ pub(crate) mod test {
599599
use std::io;
600600

601601
use assert_no_alloc::AllocDisabler;
602-
use config::Config;
602+
use config::{Config, ConfigBuilder};
603603
use once_cell::sync::Lazy;
604+
use typestate::AgentScope;
604605

605606
use super::*;
606607

@@ -988,6 +989,30 @@ pub(crate) mod test {
988989
res.body_mut().read_to_string().unwrap();
989990
}
990991

992+
#[test]
993+
fn ensure_reasonable_stack_sizes() {
994+
macro_rules! ensure {
995+
($type:ty, $size:tt) => {
996+
let sz = std::mem::size_of::<$type>();
997+
// println!("{}: {}", stringify!($type), sz);
998+
assert!(
999+
sz <= $size,
1000+
"Stack size of {} is too big {} > {}",
1001+
stringify!($type),
1002+
sz,
1003+
$size
1004+
);
1005+
};
1006+
}
1007+
1008+
ensure!(RequestBuilder<WithoutBody>, 400); // 288
1009+
ensure!(Agent, 100); // 32
1010+
ensure!(Config, 400); // 320
1011+
ensure!(ConfigBuilder<AgentScope>, 400); // 320
1012+
ensure!(Response<Body>, 800); // 760
1013+
ensure!(Body, 700); // 648
1014+
}
1015+
9911016
// This doesn't need to run, just compile.
9921017
fn _ensure_send_sync() {
9931018
fn is_send(_t: impl Send) {}

src/run.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ pub(crate) struct BodyHandler {
550550
connection: Option<Connection>,
551551
timings: CallTimings,
552552
remote_closed: bool,
553-
redirect: Option<Flow<Redirect>>,
553+
redirect: Option<Box<Flow<Redirect>>>,
554554
}
555555

556556
impl BodyHandler {
@@ -651,7 +651,7 @@ impl BodyHandler {
651651
let must_close_connection = match flow.proceed().unwrap() {
652652
RecvBodyResult::Redirect(flow) => {
653653
let c = flow.must_close_connection();
654-
self.redirect = Some(flow);
654+
self.redirect = Some(Box::new(flow));
655655
c
656656
}
657657
RecvBodyResult::Cleanup(v) => v.must_close_connection(),
@@ -674,7 +674,7 @@ impl BodyHandler {
674674

675675
// Unwrap is OK, because we are only consuming the redirect body if
676676
// such a body was signalled by the remote.
677-
let redirect = self.redirect.take();
677+
let redirect = self.redirect.take().map(|b| *b);
678678
Ok(redirect.expect("remote to have signaled redirect"))
679679
}
680680
}

src/timings.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::fmt;
22
use std::sync::Arc;
3-
use ureq_proto::ArrayVec;
43

54
use crate::config::Timeouts;
65
use crate::transport::time::{Duration, Instant};
@@ -84,35 +83,31 @@ impl Timeout {
8483

8584
#[derive(Debug)]
8685
pub(crate) struct CallTimings {
87-
timeouts: Timeouts,
86+
timeouts: Box<Timeouts>,
8887
current_time: CurrentTime,
89-
times: ArrayVec<(Timeout, Instant), 8>,
88+
times: Vec<(Timeout, Instant)>,
9089
}
9190

9291
impl Default for CallTimings {
9392
fn default() -> Self {
9493
Self {
9594
timeouts: Default::default(),
9695
current_time: Default::default(),
97-
times: empty_times(),
96+
times: vec![],
9897
}
9998
}
10099
}
101100

102-
fn empty_times() -> ArrayVec<(Timeout, Instant), 8> {
103-
ArrayVec::from_fn(|_| (Timeout::Global, Instant::AlreadyHappened))
104-
}
105-
106101
impl CallTimings {
107102
pub(crate) fn new(timeouts: Timeouts, current_time: CurrentTime) -> Self {
108-
let mut times = empty_times();
103+
let mut times = Vec::with_capacity(8);
109104

110105
let now = current_time.now();
111106
times.push((Timeout::Global, now));
112107
times.push((Timeout::PerCall, now));
113108

114109
CallTimings {
115-
timeouts,
110+
timeouts: Box::new(timeouts),
116111
current_time,
117112
times,
118113
}

0 commit comments

Comments
 (0)