double free

mail@pastecode.io avatar
unknown
rust
19 days ago
2.7 kB
6
Indexable
Never
#![allow(unused)]


use std::mem;
use std::mem::transmute;
use std::ptr;
use std::panic;
use std::error;
use std::alloc::{alloc, dealloc, Layout};
use fallible_collections::*;
pub use hashmap::*;
use std::ffi::CString;

use hashbrown::HashMap;
use std::hash::{Hash, Hasher};

use fallible_vec::{FallibleVec, try_vec};

pub fn m_with_capacity<T>(capacity: usize) -> Vec<T> {
    let size = capacity * std::mem::size_of::<T>();
    let align = std::mem::align_of::<T>();
    unsafe {
	let layout = Layout::from_size_align_unchecked(size, align);
        let ptr = alloc(layout);
        if ptr.is_null() {
            return Vec::from_raw_parts(std::ptr::null_mut(), 0, 0);
        }
        Vec::from_raw_parts(ptr as *mut T, 0, capacity)
    }
}

pub fn box_alloc(sz: usize, src: *const std::os::raw::c_void) -> *mut std::os::raw::c_void {
    unsafe {
	let layout = Layout::from_size_align_unchecked(sz, 1);
        let ptr = alloc(layout)  as *mut std::os::raw::c_void;
	if ptr.is_null() {
            return std::ptr::null_mut();
	}
	ptr::copy_nonoverlapping(src, ptr as *mut _, sz);
	return ptr;
    }
}

pub struct HTTP2Frame {
    pub header : u32,
    pub data: u32,
}

struct HTTP2Transaction {
    tx_id: u64,
    pub stream_id: u32,
    pub frames_tc: Vec<HTTP2Frame>,
    pub frames_ts: Vec<HTTP2Frame>,
    pub escaped: Vec<Vec<u8>>,
}
impl HTTP2Transaction {
    pub fn new() -> HTTP2Transaction {
        HTTP2Transaction {
            tx_id: 0,
            stream_id: 0,
            frames_tc: Vec::new(),
            frames_ts: Vec::new(),
            escaped: m_with_capacity(16),
        }
    }

    pub fn get_tx_id(&mut self) ->u64{
        return self.tx_id;
    }

    pub fn free(&mut self) {
    }
}

struct Vaisseau {
    pub chunk_count: u64,
    pub titi: u64,
    pub toto: u64,
    pub tata: u64,
    pub vec1: Vec<u64>,
    pub vec2: Vec<u64>,
    pub escaped: Vec<Vec<u8>>,
    pub transactions: Vec<HTTP2Transaction>,
}

impl Vaisseau {
    pub fn new() -> Vaisseau {
        return Vaisseau {
            chunk_count:0,
	    titi:1,
	    toto:2,
	    tata:3,
	    vec1:Vec::new(),
	    vec2:Vec::new(),
	    escaped: m_with_capacity(20),
	    transactions: Vec::new(),
        }
    }
}


fn func2(boxed: *mut std::os::raw::c_void) {
    let mut state: Box<Vaisseau> = unsafe { transmute(boxed) };

}

fn main() -> () {

    let mut vaisseau2 = Vaisseau::new();
    let boxed2 = box_alloc(std::mem::size_of::<Vaisseau>(), std::ptr::addr_of_mut!(vaisseau2) as *mut std::os::raw::c_void);
    func2(boxed2);
    println!("FIN");
}
Leave a Comment