From: Matt Corallo Date: Mon, 3 Sep 2018 21:38:25 +0000 (-0400) Subject: Ensure we aren't duplicatively reading things in router_target X-Git-Tag: v0.0.12~330^2~1 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=4ee011441b7c97cc9a1c51f3bf8b8460f54d6de0;p=rust-lightning Ensure we aren't duplicatively reading things in router_target --- diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs index f88ea9e6a..288dc56a4 100644 --- a/fuzz/fuzz_targets/router_target.rs +++ b/fuzz/fuzz_targets/router_target.rs @@ -63,6 +63,13 @@ impl InputData { } Some(&self.data[old_pos..old_pos + len]) } + fn get_slice_nonadvancing(&self, len: usize) -> Option<&[u8]> { + let old_pos = self.read_pos.load(Ordering::Acquire); + if self.data.len() < old_pos + len { + return None; + } + Some(&self.data[old_pos..old_pos + len]) + } } struct DummyChainWatcher { @@ -94,23 +101,23 @@ impl ChainWatchInterface for DummyChainWatcher { pub fn do_test(data: &[u8]) { reset_rng_state(); - let mut read_pos = 0; + let input = Arc::new(InputData { + data: data.to_vec(), + read_pos: AtomicUsize::new(0), + }); macro_rules! get_slice_nonadvancing { ($len: expr) => { - { - if data.len() < read_pos + $len as usize { - return; - } - &data[read_pos..read_pos + $len as usize] + match input.get_slice_nonadvancing($len as usize) { + Some(slice) => slice, + None => return, } } } macro_rules! get_slice { ($len: expr) => { - { - let res = get_slice_nonadvancing!($len); - read_pos += $len; - res + match input.get_slice($len as usize) { + Some(slice) => slice, + None => return, } } } @@ -153,12 +160,8 @@ pub fn do_test(data: &[u8]) { } let logger: Arc = Arc::new(test_logger::TestLogger{}); - let input = Arc::new(InputData { - data: data.to_vec(), - read_pos: AtomicUsize::new(0), - }); let chain_monitor = Arc::new(DummyChainWatcher { - input: input, + input: Arc::clone(&input), }); let our_pubkey = get_pubkey!();