Skip to content

Commit da95161

Browse files
author
p00512853
committed
Fix: Limit the number in the slab
1 parent 294000c commit da95161

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/proto/streams/counts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ impl Counts {
5050
self.num_send_streams != 0 || self.num_recv_streams != 0
5151
}
5252

53+
/// Returns the maximum number of streams.
54+
pub(crate) fn max_streams(&self) -> usize {
55+
self.max_recv_streams + self.max_reset_streams
56+
}
57+
5358
/// Returns true if the receive stream concurrency can be incremented
5459
pub fn can_inc_num_recv_streams(&self) -> bool {
5560
self.max_recv_streams > self.num_recv_streams

src/proto/streams/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl Store {
212212
self.ids.len()
213213
}
214214

215-
#[cfg(feature = "unstable")]
215+
/// Returns the number of streams that are held in slab.
216216
pub fn num_wired_streams(&self) -> usize {
217217
self.slab.len()
218218
}

src/proto/streams/streams.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,16 @@ where
311311
impl<B> DynStreams<'_, B> {
312312
pub fn recv_headers(&mut self, frame: frame::Headers) -> Result<(), Error> {
313313
let mut me = self.inner.lock().unwrap();
314-
314+
if me.store.num_wired_streams() > me.counts.max_streams() {
315+
tracing::error!(
316+
"HEADERS: number of streams exceeds the upper limit ({})",
317+
me.counts.max_streams()
318+
);
319+
return Err(Error::remote_go_away(
320+
Bytes::from("out of stream limit"),
321+
Reason::PROTOCOL_ERROR,
322+
));
323+
}
315324
me.recv_headers(self.peer, &self.send_buffer, frame)
316325
}
317326

@@ -348,6 +357,17 @@ impl<B> DynStreams<'_, B> {
348357

349358
pub fn recv_push_promise(&mut self, frame: frame::PushPromise) -> Result<(), Error> {
350359
let mut me = self.inner.lock().unwrap();
360+
361+
if me.store.num_wired_streams() > me.counts.max_streams() {
362+
tracing::error!(
363+
"PUSH_PROMISE: number of streams exceeds the upper limit ({})",
364+
me.counts.max_streams()
365+
);
366+
return Err(Error::remote_go_away(
367+
Bytes::from("out of stream limit"),
368+
Reason::PROTOCOL_ERROR,
369+
));
370+
}
351371
me.recv_push_promise(&self.send_buffer, frame)
352372
}
353373

0 commit comments

Comments
 (0)