Skip to content

Commit 454e0c5

Browse files
committed
Smaller integer for depth
1 parent 143897c commit 454e0c5

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

rmp/src/marker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ const FIXMAP_SIZE : u8 = 0x0f;
77
#[repr(u8)]
88
pub enum Marker {
99
FixPos(u8) = 0x00,
10-
FixNeg(i8) = 0xe0,
1110
FixMap(u8) = 0x80,
1211
FixArray(u8) = 0x90,
1312
FixStr(u8) = 0xa0,
1413
Null = 0xc0,
15-
// Marked in MessagePack spec as never used.
14+
/// Marked in MessagePack spec as never used.
1615
Reserved,
1716
False,
1817
True,
@@ -44,6 +43,7 @@ pub enum Marker {
4443
Array32,
4544
Map16,
4645
Map32,
46+
FixNeg(i8) = 0xe0,
4747
}
4848

4949
impl Marker {
@@ -53,7 +53,6 @@ impl Marker {
5353
pub fn from_u8(n: u8) -> Marker {
5454
match n {
5555
0x00 ..= 0x7f => Marker::FixPos(n),
56-
0xe0 ..= 0xff => Marker::FixNeg(n as i8),
5756
0x80 ..= 0x8f => Marker::FixMap(n & FIXMAP_SIZE),
5857
0x90 ..= 0x9f => Marker::FixArray(n & FIXARRAY_SIZE),
5958
0xa0 ..= 0xbf => Marker::FixStr(n & FIXSTR_SIZE),
@@ -90,6 +89,7 @@ impl Marker {
9089
0xdd => Marker::Array32,
9190
0xde => Marker::Map16,
9291
0xdf => Marker::Map32,
92+
0xe0 ..= 0xff => Marker::FixNeg(n as i8),
9393
}
9494
}
9595

rmpv/src/decode/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ pub enum Error {
2424
DepthLimitExceeded,
2525
}
2626

27-
fn decrement_depth(depth: usize) -> Result<usize, Error> {
28-
if depth == 0 {
29-
Err(Error::DepthLimitExceeded)
30-
} else {
31-
Ok(depth - 1)
32-
}
27+
#[inline]
28+
fn decrement_depth(depth: u16) -> Result<u16, Error> {
29+
depth.checked_sub(1).ok_or(Error::DepthLimitExceeded)
3330
}
3431

3532
impl Error {

rmpv/src/decode/value.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{Utf8String, Value};
1010
// See https://github.com/3Hren/msgpack-rust/issues/151
1111
const PREALLOC_MAX: usize = 64 * 1024; // 64 KiB
1212

13-
fn read_array_data<R: Read>(rd: &mut R, mut len: usize, depth: usize) -> Result<Vec<Value>, Error> {
13+
fn read_array_data<R: Read>(rd: &mut R, mut len: usize, depth: u16) -> Result<Vec<Value>, Error> {
1414
let depth = super::decrement_depth(depth)?;
1515

1616
// Note: Do not preallocate a Vec of size `len`.
@@ -25,7 +25,7 @@ fn read_array_data<R: Read>(rd: &mut R, mut len: usize, depth: usize) -> Result<
2525
Ok(vec)
2626
}
2727

28-
fn read_map_data<R: Read>(rd: &mut R, mut len: usize, depth: usize) -> Result<Vec<(Value, Value)>, Error> {
28+
fn read_map_data<R: Read>(rd: &mut R, mut len: usize, depth: u16) -> Result<Vec<(Value, Value)>, Error> {
2929
let depth = super::decrement_depth(depth)?;
3030

3131
// Note: Do not preallocate a Vec of size `len`.
@@ -40,7 +40,7 @@ fn read_map_data<R: Read>(rd: &mut R, mut len: usize, depth: usize) -> Result<Ve
4040
Ok(vec)
4141
}
4242

43-
fn read_str_data<R: Read>(rd: &mut R, len: usize, depth: usize) -> Result<Utf8String, Error> {
43+
fn read_str_data<R: Read>(rd: &mut R, len: usize, depth: u16) -> Result<Utf8String, Error> {
4444
let depth = super::decrement_depth(depth)?;
4545

4646
match String::from_utf8(read_bin_data(rd, len, depth)?) {
@@ -55,7 +55,7 @@ fn read_str_data<R: Read>(rd: &mut R, len: usize, depth: usize) -> Result<Utf8St
5555
}
5656
}
5757

58-
fn read_bin_data<R: Read>(rd: &mut R, len: usize, depth: usize) -> Result<Vec<u8>, Error> {
58+
fn read_bin_data<R: Read>(rd: &mut R, len: usize, depth: u16) -> Result<Vec<u8>, Error> {
5959
let _depth = super::decrement_depth(depth)?;
6060

6161
let mut buf = Vec::with_capacity(min(len, PREALLOC_MAX));
@@ -70,7 +70,7 @@ fn read_bin_data<R: Read>(rd: &mut R, len: usize, depth: usize) -> Result<Vec<u8
7070
Ok(buf)
7171
}
7272

73-
fn read_ext_body<R: Read>(rd: &mut R, len: usize, depth: usize) -> Result<(i8, Vec<u8>), Error> {
73+
fn read_ext_body<R: Read>(rd: &mut R, len: usize, depth: u16) -> Result<(i8, Vec<u8>), Error> {
7474
let depth = super::decrement_depth(depth)?;
7575

7676
let ty = rd.read_data_i8()?;
@@ -79,7 +79,8 @@ fn read_ext_body<R: Read>(rd: &mut R, len: usize, depth: usize) -> Result<(i8, V
7979
Ok((ty, vec))
8080
}
8181

82-
fn read_value_inner<R>(rd: &mut R, depth: usize) -> Result<Value, Error> where R: Read {
82+
#[inline(never)]
83+
fn read_value_inner<R>(rd: &mut R, depth: u16) -> Result<Value, Error> where R: Read {
8384
let depth = super::decrement_depth(depth)?;
8485
let val = match read_marker(rd)? {
8586
Marker::Null => Value::Nil,
@@ -216,11 +217,11 @@ fn read_value_inner<R>(rd: &mut R, depth: usize) -> Result<Value, Error> where R
216217
/// [`Error::DepthLimitExceeded`] is returned if this function recurses
217218
/// [`MAX_DEPTH`](super::MAX_DEPTH) times. To configure the maximum recursion depth, use
218219
/// [`read_value_with_max_depth`] instead.
219-
#[inline(never)]
220+
#[inline]
220221
pub fn read_value<R>(rd: &mut R) -> Result<Value, Error>
221222
where R: Read
222223
{
223-
read_value_inner(rd, super::MAX_DEPTH)
224+
read_value_inner(rd, super::MAX_DEPTH as _)
224225
}
225226

226227
/// Attempts to read bytes from the given reader and interpret them as a [`Value`].
@@ -234,9 +235,9 @@ pub fn read_value<R>(rd: &mut R) -> Result<Value, Error>
234235
/// [`Error::DepthLimitExceeded`] is returned if this function recurses
235236
/// `max_depth` times. If the default [`MAX_DEPTH`](super::MAX_DEPTH) is sufficient or you do not
236237
/// need recursion depth checking for your data, consider using [`read_value`] instead.
237-
#[inline(never)]
238+
#[inline]
238239
pub fn read_value_with_max_depth<R>(rd: &mut R, max_depth: usize) -> Result<Value, Error>
239240
where R: Read
240241
{
241-
read_value_inner(rd, max_depth)
242+
read_value_inner(rd, max_depth.min(u16::MAX as usize) as u16)
242243
}

rmpv/src/decode/value_ref.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rmp::Marker;
88
use super::Error;
99
use crate::{Utf8StringRef, ValueRef};
1010

11-
fn read_str_data<'a, R>(rd: &mut R, len: usize, depth: usize) -> Result<Utf8StringRef<'a>, Error>
11+
fn read_str_data<'a, R>(rd: &mut R, len: usize, depth: u16) -> Result<Utf8StringRef<'a>, Error>
1212
where R: BorrowRead<'a>
1313
{
1414
let depth = super::decrement_depth(depth)?;
@@ -24,7 +24,7 @@ fn read_str_data<'a, R>(rd: &mut R, len: usize, depth: usize) -> Result<Utf8Stri
2424
}
2525
}
2626

27-
fn read_bin_data<'a, R>(rd: &mut R, len: usize, depth: usize) -> Result<&'a [u8], Error>
27+
fn read_bin_data<'a, R>(rd: &mut R, len: usize, depth: u16) -> Result<&'a [u8], Error>
2828
where R: BorrowRead<'a>
2929
{
3030
let _depth = super::decrement_depth(depth)?;
@@ -41,7 +41,7 @@ fn read_bin_data<'a, R>(rd: &mut R, len: usize, depth: usize) -> Result<&'a [u8]
4141
Ok(buf)
4242
}
4343

44-
fn read_ext_body<'a, R>(rd: &mut R, len: usize, depth: usize) -> Result<(i8, &'a [u8]), Error>
44+
fn read_ext_body<'a, R>(rd: &mut R, len: usize, depth: u16) -> Result<(i8, &'a [u8]), Error>
4545
where R: BorrowRead<'a>
4646
{
4747
let depth = super::decrement_depth(depth)?;
@@ -51,7 +51,7 @@ fn read_ext_body<'a, R>(rd: &mut R, len: usize, depth: usize) -> Result<(i8, &'a
5151
Ok((ty, buf))
5252
}
5353

54-
fn read_array_data<'a, R>(rd: &mut R, mut len: usize, depth: usize) -> Result<Vec<ValueRef<'a>>, Error>
54+
fn read_array_data<'a, R>(rd: &mut R, mut len: usize, depth: u16) -> Result<Vec<ValueRef<'a>>, Error>
5555
where R: BorrowRead<'a>
5656
{
5757
let depth = super::decrement_depth(depth)?;
@@ -67,7 +67,7 @@ fn read_array_data<'a, R>(rd: &mut R, mut len: usize, depth: usize) -> Result<Ve
6767
Ok(vec)
6868
}
6969

70-
fn read_map_data<'a, R>(rd: &mut R, mut len: usize, depth: usize) -> Result<Vec<(ValueRef<'a>, ValueRef<'a>)>, Error>
70+
fn read_map_data<'a, R>(rd: &mut R, mut len: usize, depth: u16) -> Result<Vec<(ValueRef<'a>, ValueRef<'a>)>, Error>
7171
where R: BorrowRead<'a>
7272
{
7373
let depth = super::decrement_depth(depth)?;
@@ -127,7 +127,7 @@ impl<'a> BorrowRead<'a> for Cursor<&'a [u8]> {
127127
}
128128
}
129129

130-
fn read_value_ref_inner<'a, R>(rd: &mut R, depth: usize) -> Result<ValueRef<'a>, Error>
130+
fn read_value_ref_inner<'a, R>(rd: &mut R, depth: u16) -> Result<ValueRef<'a>, Error>
131131
where R: BorrowRead<'a>
132132
{
133133
let depth = super::decrement_depth(depth)?;
@@ -296,7 +296,7 @@ fn read_value_ref_inner<'a, R>(rd: &mut R, depth: usize) -> Result<ValueRef<'a>,
296296
pub fn read_value_ref<'a, R>(rd: &mut R) -> Result<ValueRef<'a>, Error>
297297
where R: BorrowRead<'a>
298298
{
299-
read_value_ref_inner(rd, super::MAX_DEPTH)
299+
read_value_ref_inner(rd, super::MAX_DEPTH as _)
300300
}
301301

302302
/// Attempts to read the data from the given reader until either a complete MessagePack value
@@ -316,5 +316,5 @@ pub fn read_value_ref<'a, R>(rd: &mut R) -> Result<ValueRef<'a>, Error>
316316
pub fn read_value_ref_with_max_depth<'a, R>(rd: &mut R, max_depth: usize) -> Result<ValueRef<'a>, Error>
317317
where R: BorrowRead<'a>
318318
{
319-
read_value_ref_inner(rd, max_depth)
319+
read_value_ref_inner(rd, max_depth.min(u16::MAX as _) as u16)
320320
}

0 commit comments

Comments
 (0)