Skip to content

Commit

Permalink
Added a pipeline to deny 'no_global_oom_handling'
Browse files Browse the repository at this point in the history
Added try_reserve to VecWriter, Vec, VecDeque and HashMap
Made the OOM test succeed on the latest nightly
Made the OOM check work on nightly
Disabled stable checks for now
Removed feature that is stable in current nightly
Fixed issue where Box<[T]> would not do the size limit check
Added a drop guard to `impl<T> Decode for Vec<T>` to make sure memory is not leaked when T::Decode panics
Made the implementation of `VecWriter` use less lines of unsfe
Fixed errors while merging
  • Loading branch information
VictorKoenders committed Aug 17, 2022
1 parent 3a4f299 commit bc344d2
Show file tree
Hide file tree
Showing 6 changed files with 463 additions and 202 deletions.
32 changes: 29 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"uses": "actions-rs/cargo@v1",
"with": {
"command": "check",
"args": "--all-features"
"args": "--features ci"
},
"name": "Run `cargo check`"
},
Expand Down Expand Up @@ -128,6 +128,32 @@ fi",
}
]
},
"no_oom": {
"name": "Strict OOM checks",
"runs-on": "ubuntu-latest",
"steps": [
{
"uses": "actions/checkout@v2",
"name": "Checkout"
},
{
"uses": "actions-rs/toolchain@v1",
"with": {
"profile": "minimal",
"toolchain": "nightly",
"components": "rust-src",
"override": true
},
"name": "Install Rust nightly"
},
{
"run": "cargo build --no-default-features --features unstable-strict-oom-checks -Z build-std=core,alloc --target x86_64-unknown-linux-gnu",
"env": {
"RUSTFLAGS": "--cfg no_global_oom_handling"
}
}
]
},
"lints": {
"name": "Lints",
"runs-on": "ubuntu-latest",
Expand Down Expand Up @@ -158,7 +184,7 @@ fi",
"uses": "actions-rs/cargo@v1",
"with": {
"command": "clippy",
"args": "--all-features -- -D warnings"
"args": "--features ci -- -D warnings"
},
"name": "Run `cargo clippy`"
}
Expand Down Expand Up @@ -213,7 +239,7 @@ fi",
"uses": "actions-rs/[email protected]",
"with": {
"version": "0.19.1",
"args": "--all --all-features"
"args": "--all --features ci"
}
},
{
Expand Down
21 changes: 14 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
[workspace]
members = [
"derive",
"compatibility"
]
members = ["derive", "compatibility"]

[package]
name = "bincode"
version = "2.0.0-rc.1" # remember to update html_root_url and bincode_derive
authors = ["Ty Overby <[email protected]>", "Zoey Riordan <[email protected]>", "Victor Koenders <[email protected]>"]
authors = [
"Ty Overby <[email protected]>",
"Zoey Riordan <[email protected]>",
"Victor Koenders <[email protected]>",
]
exclude = ["logo.svg", "examples/*", ".gitignore", ".github/"]

publish = true
publish = true

repository = "https://github.com/bincode-org/bincode"
documentation = "https://docs.rs/bincode"
Expand All @@ -29,6 +30,12 @@ std = ["alloc", "serde?/std"]
alloc = ["serde?/alloc"]
derive = ["bincode_derive"]

## !! Not for public use !!
# features to check again in CI
ci = ["std", "alloc", "derive", "serde"]
# experimental strict OOM checks, requires nightly features
unstable-strict-oom-checks = ["alloc"]

[dependencies]
bincode_derive = { path = "derive", version = "2.0.0-rc.1", optional = true }
serde = { version = "1.0", default-features = false, optional = true }
Expand All @@ -42,7 +49,7 @@ criterion = "0.3"
rand = "0.8"
uuid = { version = "0.8", features = ["serde"] }
chrono = { version = "0.4", features = ["serde"] }
glam = { version="0.20.5", features=["serde"] }
glam = { version = "0.20.5", features = ["serde"] }

[[bench]]
name = "varint"
Expand Down
57 changes: 57 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Errors that can be encounting by Encoding and Decoding.
#[cfg(feature = "alloc")]
use alloc::collections::TryReserveError;
#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
use core::alloc::AllocError;

/// Errors that can be encountered by encoding a type
#[non_exhaustive]
#[derive(Debug)]
Expand Down Expand Up @@ -51,6 +56,10 @@ pub enum EncodeError {
time: std::boxed::Box<std::time::SystemTime>,
},

/// bincode failed to allocate enough memory
#[cfg(feature = "alloc")]
OutOfMemory(OutOfMemory),

#[cfg(feature = "serde")]
/// A serde-specific error that occurred while decoding.
Serde(crate::features::serde::EncodeError),
Expand Down Expand Up @@ -171,6 +180,54 @@ pub enum DecodeError {
#[cfg(feature = "serde")]
/// A serde-specific error that occurred while decoding.
Serde(crate::features::serde::DecodeError),

/// bincode failed to allocate enough memory
#[cfg(feature = "alloc")]
OutOfMemory(OutOfMemory),
}

/// A wrapper to make all the out of memory errors consistent
#[cfg(feature = "alloc")]
#[derive(Debug, PartialEq)]
pub enum OutOfMemory {
/// Failed to reserve an entry
TryReserve(TryReserveError),
#[cfg(feature = "unstable-strict-oom-checks")]
/// Failed to allocate memory
Alloc(AllocError),
}

impl From<TryReserveError> for DecodeError {
fn from(e: TryReserveError) -> Self {
Self::OutOfMemory(OutOfMemory::TryReserve(e))
}
}

impl From<TryReserveError> for EncodeError {
fn from(e: TryReserveError) -> Self {
Self::OutOfMemory(OutOfMemory::TryReserve(e))
}
}

#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
impl From<AllocError> for DecodeError {
fn from(e: AllocError) -> Self {
Self::OutOfMemory(OutOfMemory::Alloc(e))
}
}

#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
impl From<AllocError> for EncodeError {
fn from(e: AllocError) -> Self {
Self::OutOfMemory(OutOfMemory::Alloc(e))
}
}

#[cfg(all(feature = "alloc", feature = "unstable-strict-oom-checks"))]
impl From<AllocError> for DecodeError {
fn from(e: AllocError) -> Self {
Self::OutOfMemory(OutOfMemory::Alloc(e))
}
}

impl core::fmt::Display for DecodeError {
Expand Down
Loading

0 comments on commit bc344d2

Please sign in to comment.