summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs49
1 files changed, 29 insertions, 20 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 2b5e3ac..b9a7b07 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,33 +1,35 @@
use std::cell::RefCell;
use libc::malloc;
-use std::fmt::{Result, Display, Formatter};
+use std::fmt::{self, Display, Formatter};
-pub mod parse;
+mod parse;
#[derive(Clone, Debug)]
-pub enum PissError {
+pub enum LeakError {
+ Unexpected(char),
InvalidSuffix(String),
InvalidNumber(String),
NumberNotPositive
}
-impl Display for PissError {
- fn fmt(&self, f: &mut Formatter<'_>) -> Result {
+impl Display for LeakError {
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
- Self::InvalidSuffix(s) => write!(f, "I cannot piss the suffix {s:?}"),
- Self::InvalidNumber(s) => write!(f, "I cannot piss the invalid number {s:?}"),
- Self::NumberNotPositive => write!(f, "I cannot piss nonexistent memory")
+ Self::Unexpected(c) => write!(f, "Unexpected character: '{c}'"),
+ Self::InvalidSuffix(s) => write!(f, "Invalid file size suffix: '{s}'"),
+ Self::InvalidNumber(s) => write!(f, "Invalid number: '{s}'"),
+ Self::NumberNotPositive => write!(f, "Cannot leak negative memory")
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
-pub struct PissAmount {
- to_piss: u128
+pub struct LeakAmount {
+ to_leak: u128
}
#[derive(Copy, Clone, Debug)]
-pub enum Toliet {
+pub enum LeakMethod {
Rust,
Lazy,
Unsafe
@@ -42,7 +44,7 @@ thread_local!(static TOLIET: RefCell<Vec<Vec<u8>>> = RefCell::new(Vec::new()));
fn leak_lazy(amount: usize) {
TOLIET.with(|t| {
t.borrow_mut().push(Vec::<u8>::with_capacity(amount));
- })
+ });
}
fn leak_unsafe(amount: usize) {
@@ -51,16 +53,21 @@ fn leak_unsafe(amount: usize) {
}
}
-fn leak_match(amount: usize, method: Toliet) {
+fn leak_match(amount: usize, method: LeakMethod) {
match method {
- Toliet::Rust => leak_rust(amount),
- Toliet::Lazy => leak_lazy(amount),
- Toliet::Unsafe => leak_unsafe(amount),
+ LeakMethod::Rust => leak_rust(amount),
+ LeakMethod::Lazy => leak_lazy(amount),
+ LeakMethod::Unsafe => leak_unsafe(amount),
}
}
-pub fn leak<T: Into<PissAmount>>(amount: T, method: Toliet) {
- let num: u128 = amount.into().to_piss;
+pub fn leak<T: Into<LeakAmount>>(amount: T, method: LeakMethod) -> Result<(), LeakError> {
+ let num: u128 = amount.into().to_leak;
+
+ if num <= 0 {
+ return Err(LeakError::NumberNotPositive)
+ }
+
let count = num / usize::MAX as u128;
for _ in 0..count {
@@ -68,8 +75,10 @@ pub fn leak<T: Into<PissAmount>>(amount: T, method: Toliet) {
}
leak_match((num % usize::MAX as u128) as usize, method);
+
+ Ok(())
}
-pub async fn leak_async<T: Into<PissAmount>>(amount: T, method: Toliet) {
- leak(amount, method);
+pub async fn leak_async<T: Into<LeakAmount> + Send>(amount: T, method: LeakMethod) -> Result<(), LeakError> {
+ leak(amount, method)
}