Skip to content

Modify the buffer position directly when reading leb128 values. #92631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions compiler/rustc_serialize/src/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ impl_write_unsigned_leb128!(write_usize_leb128, usize);
macro_rules! impl_read_unsigned_leb128 {
($fn_name:ident, $int_ty:ty) => {
#[inline]
pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
let mut result = 0;
let mut shift = 0;
let mut position = 0;
loop {
let byte = slice[position];
position += 1;
let byte = slice[*position];
*position += 1;
if (byte & 0x80) == 0 {
result |= (byte as $int_ty) << shift;
return (result, position);
return result;
} else {
result |= ((byte & 0x7F) as $int_ty) << shift;
}
Expand Down Expand Up @@ -122,15 +121,14 @@ impl_write_signed_leb128!(write_isize_leb128, isize);
macro_rules! impl_read_signed_leb128 {
($fn_name:ident, $int_ty:ty) => {
#[inline]
pub fn $fn_name(slice: &[u8]) -> ($int_ty, usize) {
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
let mut result = 0;
let mut shift = 0;
let mut position = 0;
let mut byte;

loop {
byte = slice[position];
position += 1;
byte = slice[*position];
*position += 1;
result |= <$int_ty>::from(byte & 0x7F) << shift;
shift += 7;

Expand All @@ -144,7 +142,7 @@ macro_rules! impl_read_signed_leb128 {
result |= (!0 << shift);
}

(result, position)
result
}
};
}
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,7 @@ impl<'a> Decoder<'a> {
}

macro_rules! read_leb128 {
($dec:expr, $fun:ident) => {{
let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position..]);
$dec.position += bytes_read;
Ok(value)
}};
($dec:expr, $fun:ident) => {{ Ok(leb128::$fun($dec.data, &mut $dec.position)) }};
}

impl<'a> serialize::Decoder for Decoder<'a> {
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_serialize/tests/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ macro_rules! impl_test_unsigned_leb128 {

let mut position = 0;
for &expected in &values {
let (actual, bytes_read) = $read_fn_name(&stream[position..]);
let actual = $read_fn_name(&stream, &mut position);
assert_eq!(expected, actual);
position += bytes_read;
}
assert_eq!(stream.len(), position);
}
Expand Down Expand Up @@ -77,9 +76,8 @@ macro_rules! impl_test_signed_leb128 {

let mut position = 0;
for &expected in &values {
let (actual, bytes_read) = $read_fn_name(&stream[position..]);
let actual = $read_fn_name(&stream, &mut position);
assert_eq!(expected, actual);
position += bytes_read;
}
assert_eq!(stream.len(), position);
}
Expand Down