From e8d6173734b0fb43bf7401fdbe43258d913a6284 Mon Sep 17 00:00:00 2001 From: Aaron Riekenberg Date: Sun, 1 Oct 2017 11:49:36 -0500 Subject: [PATCH] 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. --- src/proto/body.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/proto/body.rs b/src/proto/body.rs index 7503ee4cc4..27fb34ec78 100644 --- a/src/proto/body.rs +++ b/src/proto/body.rs @@ -98,10 +98,9 @@ impl From<&'static [u8]> for Body { impl From> for Body { #[inline] fn from (cow: Cow<'static, [u8]>) -> Body { - if let Cow::Borrowed(value) = cow { - Body::from(value) - } else { - Body::from(cow.to_owned()) + match cow { + Cow::Borrowed(b) => Body::from(b), + Cow::Owned(o) => Body::from(o) } } } @@ -123,10 +122,9 @@ impl From<&'static str> for Body { impl From> for Body { #[inline] fn from(cow: Cow<'static, str>) -> Body { - if let Cow::Borrowed(value) = cow { - Body::from(value) - } else { - Body::from(cow.to_owned()) + match cow { + Cow::Borrowed(b) => Body::from(b), + Cow::Owned(o) => Body::from(o) } } }