Skip to content

Commit e8e2f58

Browse files
committed
Further bias against invalid input by boxing errors
1 parent 08650bd commit e8e2f58

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

src/de.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ impl<'de> Decoder<'de> {
1515

1616
macro_rules! impl_decode {
1717
($method:ident($ty:ty)) => {
18-
fn $method(&mut self) -> Result<$ty, Error> {
18+
fn $method(&mut self) -> Result<$ty, Box<Error>> {
1919
let (bytes, rest) = self.buf.split_at_checked(size_of::<$ty>()).ok_or_else(|| {
2020
cold();
21-
Error::MissingData
21+
Box::new(Error::MissingData)
2222
})?;
2323
self.buf = rest;
2424

@@ -45,12 +45,12 @@ impl Decoder<'_> {
4545
impl_decode!(decode_f32(f32));
4646
impl_decode!(decode_f64(f64));
4747

48-
fn decode_bytes(&mut self) -> Result<&[u8], Error> {
48+
fn decode_bytes(&mut self) -> Result<&[u8], Box<Error>> {
4949
let len = self.decode_u32()?;
5050

5151
let (bytes, rest) = self.buf.split_at_checked(len as usize).ok_or_else(|| {
5252
cold();
53-
Error::MissingData
53+
Box::new(Error::MissingData)
5454
})?;
5555
self.buf = rest;
5656

@@ -72,27 +72,30 @@ macro_rules! impl_deserialize {
7272
}
7373

7474
impl<'de> serde::de::Deserializer<'de> for &mut Decoder<'de> {
75-
type Error = Error;
75+
type Error = Box<Error>;
7676

7777
fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
7878
where
7979
V: serde::de::Visitor<'de>,
8080
{
81-
Err(Error::NotSupported)
81+
cold();
82+
Err(Box::new(Error::NotSupported))
8283
}
8384

8485
fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
8586
where
8687
V: serde::de::Visitor<'de>,
8788
{
88-
Err(Error::NotSupported)
89+
cold();
90+
Err(Box::new(Error::NotSupported))
8991
}
9092

9193
fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
9294
where
9395
V: serde::de::Visitor<'de>,
9496
{
95-
Err(Error::NotSupported)
97+
cold();
98+
Err(Box::new(Error::NotSupported))
9699
}
97100

98101
impl_deserialize!(deserialize_i8(i8): decode_i8 => visit_i8);
@@ -119,7 +122,7 @@ impl<'de> serde::de::Deserializer<'de> for &mut Decoder<'de> {
119122
1 => visitor.visit_bool(true),
120123
_ => {
121124
cold();
122-
Err(Error::InvalidBool)
125+
Err(Box::new(Error::InvalidBool))
123126
}
124127
}
125128
}
@@ -132,7 +135,7 @@ impl<'de> serde::de::Deserializer<'de> for &mut Decoder<'de> {
132135

133136
let value = char::from_u32(bits).ok_or_else(|| {
134137
cold();
135-
Error::InvalidChar
138+
Box::new(Error::InvalidChar)
136139
})?;
137140

138141
visitor.visit_char(value)
@@ -178,7 +181,7 @@ impl<'de> serde::de::Deserializer<'de> for &mut Decoder<'de> {
178181

179182
let value = from_utf8(bytes).map_err(|_err| {
180183
cold();
181-
Error::InvalidStr
184+
Box::new(Error::InvalidStr)
182185
})?;
183186

184187
visitor.visit_string(value.to_owned())
@@ -222,7 +225,7 @@ impl<'de> serde::de::Deserializer<'de> for &mut Decoder<'de> {
222225
1 => visitor.visit_some(self),
223226
_ => {
224227
cold();
225-
Err(Error::InvalidOption)
228+
Err(Box::new(Error::InvalidOption))
226229
}
227230
}
228231
}
@@ -302,7 +305,7 @@ struct LimitedDecoder<'a, 'de> {
302305
}
303306

304307
impl<'de> serde::de::SeqAccess<'de> for LimitedDecoder<'_, 'de> {
305-
type Error = Error;
308+
type Error = Box<Error>;
306309

307310
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
308311
where
@@ -326,7 +329,7 @@ impl<'de> serde::de::SeqAccess<'de> for LimitedDecoder<'_, 'de> {
326329
}
327330

328331
impl<'de> serde::de::MapAccess<'de> for LimitedDecoder<'_, 'de> {
329-
type Error = Error;
332+
type Error = Box<Error>;
330333

331334
fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
332335
where
@@ -357,7 +360,7 @@ impl<'de> serde::de::MapAccess<'de> for LimitedDecoder<'_, 'de> {
357360
}
358361

359362
impl<'de> serde::de::EnumAccess<'de> for &mut Decoder<'de> {
360-
type Error = Error;
363+
type Error = Box<Error>;
361364
type Variant = Self;
362365

363366
fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
@@ -366,7 +369,8 @@ impl<'de> serde::de::EnumAccess<'de> for &mut Decoder<'de> {
366369
{
367370
let variant_index = self.decode_u32()?;
368371

369-
let deserializer = serde::de::IntoDeserializer::into_deserializer(variant_index);
372+
let deserializer =
373+
serde::de::IntoDeserializer::<Self::Error>::into_deserializer(variant_index);
370374

371375
let value = seed.deserialize(deserializer)?;
372376

@@ -375,7 +379,7 @@ impl<'de> serde::de::EnumAccess<'de> for &mut Decoder<'de> {
375379
}
376380

377381
impl<'de> serde::de::VariantAccess<'de> for &mut Decoder<'de> {
378-
type Error = Error;
382+
type Error = Box<Error>;
379383

380384
fn unit_variant(self) -> Result<(), Self::Error> {
381385
Ok(())

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt;
77
use de::Decoder;
88
use ser::Encoder;
99

10-
pub fn serialize<T>(value: &T) -> Result<Vec<u8>, Error>
10+
pub fn serialize<T>(value: &T) -> Result<Vec<u8>, Box<Error>>
1111
where
1212
T: serde::ser::Serialize,
1313
{
@@ -16,15 +16,15 @@ where
1616
Ok(buf)
1717
}
1818

19-
pub fn serialize_into<T>(buf: &mut Vec<u8>, value: &T) -> Result<(), Error>
19+
pub fn serialize_into<T>(buf: &mut Vec<u8>, value: &T) -> Result<(), Box<Error>>
2020
where
2121
T: serde::ser::Serialize,
2222
{
2323
value.serialize(Encoder::new(buf))?;
2424
Ok(())
2525
}
2626

27-
pub fn deserialize<'de, T>(buf: &'de [u8]) -> Result<T, Error>
27+
pub fn deserialize<'de, T>(buf: &'de [u8]) -> Result<T, Box<Error>>
2828
where
2929
T: serde::de::Deserialize<'de>,
3030
{
@@ -58,20 +58,20 @@ impl fmt::Display for Error {
5858

5959
impl StdError for Error {}
6060

61-
impl serde::ser::Error for Error {
61+
impl serde::ser::Error for Box<Error> {
6262
fn custom<T>(msg: T) -> Self
6363
where
6464
T: fmt::Display,
6565
{
66-
Self::Custom(msg.to_string())
66+
Box::new(Error::Custom(msg.to_string()))
6767
}
6868
}
6969

70-
impl serde::de::Error for Error {
70+
impl serde::de::Error for Box<Error> {
7171
fn custom<T>(msg: T) -> Self
7272
where
7373
T: fmt::Display,
7474
{
75-
Self::Custom(msg.to_string())
75+
Box::new(Error::Custom(msg.to_string()))
7676
}
7777
}

src/ser.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a> Encoder<'a> {
1515
}
1616
}
1717

18-
fn serialize_len(self, len: usize) -> Result<Self, Error> {
18+
fn serialize_len(self, len: usize) -> Result<Self, Box<Error>> {
1919
serde::ser::Serializer::serialize_u32(self, len.try_into().expect("Excessive length"))
2020
}
2121
}
@@ -31,7 +31,7 @@ macro_rules! impl_serialize {
3131

3232
impl serde::ser::Serializer for Encoder<'_> {
3333
type Ok = Self;
34-
type Error = Error;
34+
type Error = Box<Error>;
3535

3636
type SerializeSeq = Self;
3737
type SerializeTuple = Self;
@@ -187,7 +187,7 @@ impl serde::ser::Serializer for Encoder<'_> {
187187

188188
impl serde::ser::SerializeSeq for Encoder<'_> {
189189
type Ok = Self;
190-
type Error = Error;
190+
type Error = Box<Error>;
191191

192192
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
193193
where
@@ -204,7 +204,7 @@ impl serde::ser::SerializeSeq for Encoder<'_> {
204204

205205
impl serde::ser::SerializeTuple for Encoder<'_> {
206206
type Ok = Self;
207-
type Error = Error;
207+
type Error = Box<Error>;
208208

209209
fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
210210
where
@@ -221,7 +221,7 @@ impl serde::ser::SerializeTuple for Encoder<'_> {
221221

222222
impl serde::ser::SerializeTupleStruct for Encoder<'_> {
223223
type Ok = Self;
224-
type Error = Error;
224+
type Error = Box<Error>;
225225

226226
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
227227
where
@@ -238,7 +238,7 @@ impl serde::ser::SerializeTupleStruct for Encoder<'_> {
238238

239239
impl serde::ser::SerializeTupleVariant for Encoder<'_> {
240240
type Ok = Self;
241-
type Error = Error;
241+
type Error = Box<Error>;
242242

243243
fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
244244
where
@@ -255,7 +255,7 @@ impl serde::ser::SerializeTupleVariant for Encoder<'_> {
255255

256256
impl serde::ser::SerializeMap for Encoder<'_> {
257257
type Ok = Self;
258-
type Error = Error;
258+
type Error = Box<Error>;
259259

260260
fn serialize_key<T>(&mut self, value: &T) -> Result<(), Self::Error>
261261
where
@@ -280,7 +280,7 @@ impl serde::ser::SerializeMap for Encoder<'_> {
280280

281281
impl serde::ser::SerializeStruct for Encoder<'_> {
282282
type Ok = Self;
283-
type Error = Error;
283+
type Error = Box<Error>;
284284

285285
fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<(), Self::Error>
286286
where
@@ -297,7 +297,7 @@ impl serde::ser::SerializeStruct for Encoder<'_> {
297297

298298
impl serde::ser::SerializeStructVariant for Encoder<'_> {
299299
type Ok = Self;
300-
type Error = Error;
300+
type Error = Box<Error>;
301301

302302
fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<(), Self::Error>
303303
where

0 commit comments

Comments
 (0)