Skip to content

Commit

Permalink
Merge pull request #506 from stevenroose/avoid-assert
Browse files Browse the repository at this point in the history
Avoid a few assertions that shouldn't be necessary
  • Loading branch information
apoelstra committed Oct 26, 2020
2 parents 76a1a32 + e07ee51 commit 46fdee5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

# 0.25.1 - 2020-10-26

- Remove an incorrect `debug_assert` that can cause a panic when running using
the dev profile.

# 0.25.1 - 2020-10-07

- [Expose methods on `Script`](https://github.com/rust-bitcoin/rust-bitcoin/pull/387) to generate various scriptpubkeys
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoin"
version = "0.25.1"
version = "0.25.2"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/rust-bitcoin/rust-bitcoin/"
Expand Down
2 changes: 1 addition & 1 deletion src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Script {
/// Generates P2WSH-type of scriptPubkey with a given hash of the redeem script
pub fn new_witness_program(ver: ::bech32::u5, program: &[u8]) -> Script {
let mut verop = ver.to_u8();
assert!(verop <= 16);
assert!(verop <= 16, "incorrect witness version provided: {}", verop);
if verop > 0 {
verop = 0x50 + verop;
}
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl From<psbt::Error> for Error {
pub fn serialize<T: Encodable + ?Sized>(data: &T) -> Vec<u8> {
let mut encoder = Vec::new();
let len = data.consensus_encode(&mut encoder).unwrap();
assert_eq!(len, encoder.len());
debug_assert_eq!(len, encoder.len());
encoder
}

Expand Down Expand Up @@ -249,7 +249,7 @@ macro_rules! decoder_fn {
($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
#[inline]
fn $name(&mut self) -> Result<$val_type, Error> {
assert_eq!(::std::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22
debug_assert_eq!(::std::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22
let mut val = [0; $byte_len];
self.read_exact(&mut val[..]).map_err(Error::Io)?;
Ok(endian::$readfn(&val))
Expand Down
4 changes: 2 additions & 2 deletions src/util/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! define_be_to_array {
($name: ident, $type: ty, $byte_len: expr) => {
#[inline]
pub fn $name(val: $type) -> [u8; $byte_len] {
assert_eq!(::std::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
debug_assert_eq!(::std::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
let mut res = [0; $byte_len];
for i in 0..$byte_len {
res[i] = ((val >> ($byte_len - i - 1)*8) & 0xff) as u8;
Expand All @@ -41,7 +41,7 @@ macro_rules! define_le_to_array {
($name: ident, $type: ty, $byte_len: expr) => {
#[inline]
pub fn $name(val: $type) -> [u8; $byte_len] {
assert_eq!(::std::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
debug_assert_eq!(::std::mem::size_of::<$type>(), $byte_len); // size_of isn't a constfn in 1.22
let mut res = [0; $byte_len];
for i in 0..$byte_len {
res[i] = ((val >> i*8) & 0xff) as u8;
Expand Down
3 changes: 1 addition & 2 deletions src/util/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ impl PublicKey {

/// Write the public key into a writer
pub fn write_into<W: io::Write>(&self, mut writer: W) {
let write_res: io::Result<()> = if self.compressed {
let _: io::Result<()> = if self.compressed {
writer.write_all(&self.key.serialize())
} else {
writer.write_all(&self.key.serialize_uncompressed())
};
debug_assert!(write_res.is_ok());
}

/// Serialize the public key to bytes
Expand Down
7 changes: 5 additions & 2 deletions src/util/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ macro_rules! construct_uint {
/// Create an object from a given signed 64-bit integer
#[inline]
pub fn from_i64(init: i64) -> Option<$name> {
assert!(init >= 0);
$name::from_u64(init as u64)
if init >= 0 {
$name::from_u64(init as u64)
} else {
None
}
}

/// Creates big integer value from a byte slice array using
Expand Down

0 comments on commit 46fdee5

Please sign in to comment.