Skip to content

Commit e8d6173

Browse files
aaronriekenbergseanmonstar
authored andcommitted
fix(http): avoid infinite recursion when Body::from is called with Cow::Owned. (#1343)
When cow is a Cow::Owned, cow.to_owned() returns a Cow::Owned, which leads to infinite recursion. Extract the owned or borrowed values from the cow to ensure progress is made in either case.
1 parent 9c80fdb commit e8d6173

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/proto/body.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ impl From<&'static [u8]> for Body {
9898
impl From<Cow<'static, [u8]>> for Body {
9999
#[inline]
100100
fn from (cow: Cow<'static, [u8]>) -> Body {
101-
if let Cow::Borrowed(value) = cow {
102-
Body::from(value)
103-
} else {
104-
Body::from(cow.to_owned())
101+
match cow {
102+
Cow::Borrowed(b) => Body::from(b),
103+
Cow::Owned(o) => Body::from(o)
105104
}
106105
}
107106
}
@@ -123,10 +122,9 @@ impl From<&'static str> for Body {
123122
impl From<Cow<'static, str>> for Body {
124123
#[inline]
125124
fn from(cow: Cow<'static, str>) -> Body {
126-
if let Cow::Borrowed(value) = cow {
127-
Body::from(value)
128-
} else {
129-
Body::from(cow.to_owned())
125+
match cow {
126+
Cow::Borrowed(b) => Body::from(b),
127+
Cow::Owned(o) => Body::from(o)
130128
}
131129
}
132130
}

0 commit comments

Comments
 (0)