Skip to content
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

fix: type check int for dirty high bytes #47

Merged
merged 1 commit into from
May 17, 2023
Merged
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
31 changes: 31 additions & 0 deletions crates/sol-types/src/coder/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,35 @@ mod tests {
MyTy::decode_params(&input, true).unwrap_err();
assert!(MyTy2::decode_params(&input, true).is_ok());
}

#[test]
fn signed_int_dirty_high_bytes() {
type MyTy = sol_data::Int<8>;

let dirty_negative =
hex!("f0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

assert_eq!(MyTy::decode_single(&dirty_negative, false).unwrap(), -1);

assert!(
matches!(
MyTy::decode_single(&dirty_negative, true),
Err(crate::Error::TypeCheckFail { .. }),
),
"did not match error"
);

let dirty_positive =
hex!("700000000000000000000000000000000000000000000000000000000000007f");

assert_eq!(MyTy::decode_single(&dirty_positive, false).unwrap(), 127);

assert!(
matches!(
MyTy::decode_single(&dirty_positive, true),
Err(crate::Error::TypeCheckFail { .. }),
),
"did not match error"
);
}
}
45 changes: 40 additions & 5 deletions crates/sol-types/src/types/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,25 @@ macro_rules! impl_int_sol_type {
}

#[inline]
fn type_check(_token: &Self::TokenType) -> Result<()> {
Ok(())
fn type_check(token: &Self::TokenType) -> Result<()> {
// check for 256 can be omitted as this macro expansion is not used for 256 bits

let bytes = token.as_slice();
let meaningful_idx = 32 - ($bits / 8);

let sign_extension = if bytes[meaningful_idx] & 0x80 == 0x80 {
0xff
} else {
0
};

// check that all upper bytes are an extension of the sign bit
bytes
.iter()
.take(meaningful_idx)
.all(|byte| *byte == sign_extension)
.then(|| ())
.ok_or_else(|| Self::type_check_fail(bytes))
}

#[inline]
Expand Down Expand Up @@ -173,9 +190,27 @@ macro_rules! impl_int_sol_type {
}

#[inline]
fn type_check(_token: &Self::TokenType) -> Result<()> {
// TODO
Ok(())
fn type_check(token: &Self::TokenType) -> Result<()> {
if $bits == 256 {
return Ok(())
}

let bytes = token.as_slice();
let meaningful_idx = 32 - ($bits / 8);

let sign_extension = if bytes[meaningful_idx] & 0x80 == 0x80 {
0xff
} else {
0
};

// check that all upper bytes are an extension of the sign bit
bytes
.iter()
.take(meaningful_idx)
.all(|byte| *byte == sign_extension)
.then(|| ())
.ok_or_else(|| Self::type_check_fail(bytes))
}

#[inline]
Expand Down