This tests buffer overflow for the basic types. It does not currently test the platform int and uint until the final function definitions are determined.
108 lines
2.5 KiB
Rust
108 lines
2.5 KiB
Rust
#![feature(zero_one)]
|
|
#![feature(num_bits_bytes)]
|
|
|
|
extern crate rand;
|
|
|
|
extern crate alchemy;
|
|
extern crate sigils;
|
|
|
|
|
|
|
|
use rand::thread_rng;
|
|
|
|
|
|
|
|
/// This should only be called by the macro below.
|
|
macro_rules! overflow_impl
|
|
{
|
|
($numBytes: expr, $T: ident,
|
|
$read_func: ident, $write_func: ident) =>
|
|
{
|
|
use std::num::Zero;
|
|
|
|
use alchemy::{BigEndian, LittleEndian, Transmutable};
|
|
|
|
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn read_big_endian()
|
|
{
|
|
let buffer: [u8; $numBytes];
|
|
|
|
buffer = [0u8; $numBytes];
|
|
BigEndian::$read_func(&buffer);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn read_little_endian()
|
|
{
|
|
let buffer: [u8; $numBytes];
|
|
|
|
buffer = [0u8; $numBytes];
|
|
LittleEndian::$read_func(&buffer);
|
|
}
|
|
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn write_big_endian()
|
|
{
|
|
let mut buffer: [u8; $numBytes];
|
|
|
|
buffer = [0u8; $numBytes];
|
|
BigEndian::$write_func(&mut buffer, $T::zero());
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn write_little_endian()
|
|
{
|
|
let mut buffer: [u8; $numBytes];
|
|
|
|
buffer = [0u8; $numBytes];
|
|
LittleEndian::$write_func(&mut buffer, $T::zero());
|
|
}
|
|
};
|
|
}
|
|
|
|
/// This macro tries to test for buffer overflow happening.
|
|
macro_rules! test_buffer_overflow
|
|
{
|
|
($mod_name: ident, $T: ident,
|
|
$read_func: ident, $write_func: ident) =>
|
|
{
|
|
mod $mod_name
|
|
{
|
|
use std::$T;
|
|
overflow_impl!($T::BYTES, $T, $read_func, $write_func);
|
|
}
|
|
};
|
|
|
|
($mod_name: ident, $T: ident,
|
|
$read_func: ident, $write_func: ident, $numBytes: expr) =>
|
|
{
|
|
mod $mod_name
|
|
{
|
|
overflow_impl!($numBytes, $T, $read_func, $write_func);
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
// Test the different data types for buffer overflow.
|
|
test_buffer_overflow!(overflow_u16, u16, bytes_to_u16, u16_to_bytes);
|
|
test_buffer_overflow!(overflow_u32, u32, bytes_to_u32, u32_to_bytes);
|
|
test_buffer_overflow!(overflow_u64, u64, bytes_to_u64, u64_to_bytes);
|
|
//test_buffer_overflow!(overflow_usize, usize, bytes_to_usize, usize_to_bytes);
|
|
|
|
test_buffer_overflow!(overflow_i16, i16, bytes_to_i16, i16_to_bytes);
|
|
test_buffer_overflow!(overflow_i32, i32, bytes_to_i32, i32_to_bytes);
|
|
test_buffer_overflow!(overflow_i64, i64, bytes_to_i64, i64_to_bytes);
|
|
//test_buffer_overflow!(overflow_isize, isize, bytes_to_isize, isize_to_bytes);
|
|
|
|
test_buffer_overflow!(overflow_f32, f32, bytes_to_f32, f32_to_bytes, 4);
|
|
test_buffer_overflow!(overflow_f64, f64, bytes_to_f64, f64_to_bytes, 8);
|