Merge pull request #32 from adetaylor/fuzz

Add a fuzzer.
This commit is contained in:
David Tolnay 2022-05-15 13:40:06 -07:00 committed by GitHub
commit 0d042acb34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 0 deletions

24
fuzz/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "itoa-fuzz"
version = "0.0.0"
publish = false
edition = "2018"
[package.metadata]
cargo-fuzz = true
[dependencies]
libfuzzer-sys = { version="0.4", features=["arbitrary-derive"] }
[dependencies.itoa]
path = ".."
# Prevent this from interfering with workspaces
[workspace]
members = ["."]
[[bin]]
name = "fuzz_itoa"
path = "fuzz_targets/fuzz_itoa.rs"
test = false
doc = false

View File

@ -0,0 +1,44 @@
#![no_main]
use libfuzzer_sys::arbitrary;
use libfuzzer_sys::fuzz_target;
#[derive(arbitrary::Arbitrary, Debug, Clone)]
enum IntegerInput {
I8(i8),
U8(u8),
I16(i16),
U16(u16),
I32(i32),
U32(u32),
I64(i64),
U64(u64),
ISIZE(isize),
USIZE(usize),
I128(i128),
U128(u128),
}
#[derive(arbitrary::Arbitrary, Debug, Clone)]
struct Inputs {
inputs: Vec<IntegerInput>,
}
fuzz_target!(|input: Inputs| {
let mut buffer = itoa::Buffer::new();
for input_integer in input.inputs {
match input_integer {
IntegerInput::I8(val) => buffer.format(val),
IntegerInput::U8(val) => buffer.format(val),
IntegerInput::I16(val) => buffer.format(val),
IntegerInput::U16(val) => buffer.format(val),
IntegerInput::I32(val) => buffer.format(val),
IntegerInput::U32(val) => buffer.format(val),
IntegerInput::I64(val) => buffer.format(val),
IntegerInput::U64(val) => buffer.format(val),
IntegerInput::ISIZE(val) => buffer.format(val),
IntegerInput::USIZE(val) => buffer.format(val),
IntegerInput::I128(val) => buffer.format(val),
IntegerInput::U128(val) => buffer.format(val),
};
}
});