Created
June 16, 2017 21:57
-
-
Save SalmonProject/33f006a7caf08677df20a9b12cdfc84f to your computer and use it in GitHub Desktop.
Rust VecDeque "wraparound" bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
use std::collections::VecDeque; | |
fn handle_ack(q: &mut VecDeque<u8>, bytes_ackd: u32) | |
{ | |
for _i in 0..bytes_ackd | |
{ | |
q.pop_front(); | |
} | |
} | |
fn extend_and_check(q: &mut VecDeque<u8>, amount: usize) | |
{ | |
let (size1a, size1b) = { let (a,b) = q.as_slices(); (a.len(), b.len()) }; | |
if size1b != 0 | |
{ | |
panic!("bizarreness BEFORE: {}, {}", size1a, size1b); | |
} | |
let mut raw = [0u8 ; 80000]; | |
for i in 0..amount | |
{ | |
raw[i] = (i % 256) as u8; | |
} | |
q.extend(&raw[0..amount]); | |
let (size2a, size2b) = { let (a,b) = q.as_slices(); (a.len(), b.len()) }; | |
if size2b != 0 | |
{ | |
panic!("bizarreness AFTER: {}, {}", size2a, size2b); | |
} | |
} | |
fn print_lens(q: &VecDeque<u8>) | |
{ | |
let (a,b) = q.as_slices(); | |
println!("{}\t{}", a.len(), b.len()); | |
} | |
fn random_up_to_len(q: &VecDeque<u8>) -> u32 | |
{ | |
let raw_amt = (rand::random::<u16>() % 20000) as u32; | |
let (a,_b) = q.as_slices(); | |
let q_len = a.len() as u32; | |
if raw_amt > q_len { q_len } else { raw_amt } | |
} | |
fn main() | |
{ | |
let mut q = VecDeque::with_capacity(1460*50); | |
loop | |
{ | |
if rand::random::<bool>() | |
{ | |
extend_and_check(&mut q, (rand::random::<u16>() % 20000) as usize); | |
} | |
else | |
{ | |
let ack_len = random_up_to_len(&q) as u32; | |
handle_ack(&mut q, ack_len); | |
} | |
print_lens(&q); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment