Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 131 additions & 17 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,158 @@ name: CI

on:
push:
branches: [main, master]
pull_request:

env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: -D warnings

jobs:
fmt:
name: Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt

- name: Check formatting
run: cargo fmt --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: clippy

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}

- name: Run Clippy
run: cargo clippy --all-targets --no-deps -- -D warnings

test:
name: Tests
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-test-${{ hashFiles('**/Cargo.lock') }}

- name: Run tests
run: cargo test --all-features

coverage:
name: Code Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools-preview

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-coverage-${{ hashFiles('**/Cargo.lock') }}

- name: Generate coverage report
run: cargo llvm-cov --all-features --lcov --output-path lcov.info

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
toolchain: stable
override: true
components: rustfmt, clippy
files: lcov.info
fail_ci_if_error: false
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Lint
- name: Check coverage threshold
run: |
cargo fmt -- --check
cargo clippy --all-targets --no-deps
cargo llvm-cov --all-features --fail-under-lines 90

- name: Build Documentation
run: cargo doc --no-deps
doc:
name: Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Run tests
run: cargo test
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-doc-${{ hashFiles('**/Cargo.lock') }}

minimum-supported-rust-version:
- name: Build documentation
run: cargo doc --no-deps
env:
RUSTDOCFLAGS: -D warnings

msrv:
name: Minimum Supported Rust Version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.78.0
override: true
- run: cargo check
toolchain: "1.78.0"

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-msrv-${{ hashFiles('**/Cargo.lock') }}

- name: Check MSRV
run: cargo check --all-features
28 changes: 19 additions & 9 deletions .github/workflows/rustdoc.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
name: rustdoc
name: Deploy Documentation

on:
push:
branches: [ master ]
branches: [main, master]

env:
CARGO_INCREMENTAL: 0
RUSTFLAGS: -D warnings

jobs:
rustdoc:
deploy-docs:
name: Deploy Documentation
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable

- name: Cache cargo registry
uses: actions/cache@v4
with:
toolchain: stable
override: true
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-doc-${{ hashFiles('**/Cargo.lock') }}

- name: Build Documentation
- name: Build documentation
run: cargo doc --no-deps
env:
RUSTDOCFLAGS: -D warnings

- name: Deploy Docs
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
38 changes: 38 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
# Changelog

## [Unreleased]

### Security

- **Memory amplification protection**: Deserialization now validates that claimed sequence/string lengths are plausible given the remaining input, preventing DoS attacks where a small malicious payload could trigger large memory allocations.
- **Duplicate map key detection**: Serialization now returns `Error::NonCanonicalMap` when duplicate keys are encountered instead of silently dropping duplicates, ensuring data integrity.

### Added

- `to_bytes_with_capacity()` function for pre-allocating output buffers when the serialized size is known or estimated, reducing allocations.
- Comprehensive `# Errors` documentation sections on all public functions.
- `#[must_use]` attribute on `is_human_readable()`.
- Explicit `#![forbid(unsafe_code)]` via Cargo.toml lints section.
- Full pedantic clippy lint compliance with minimal, justified exceptions for binary serialization casts.
- `rustfmt.toml` configuration for consistent code formatting.

### Changed

- **Optimized ULEB128 encoding/decoding**: Added fast paths for single-byte values (0-127), which are common for sequence lengths and enum variant indices.
- **Optimized bulk byte reading**: Deserialization now uses slice splitting instead of byte-by-byte copying for integer parsing.
- **Added `#[inline]` hints** on hot serialization/deserialization paths for better performance.
- Replaced `sort_by` with `sort_unstable_by` for map key sorting (faster, no stability needed for unique keys).

### CI/CD

- Added separate CI jobs for formatting (`cargo fmt`), linting (`cargo clippy`), testing, coverage, documentation, and MSRV verification.
- Added code coverage reporting with Codecov integration and 90% line coverage threshold.
- Added Minimum Supported Rust Version (MSRV) check at Rust 1.78.0.
- Improved CI caching for faster builds.
- Documentation builds now use `-D warnings` to catch doc issues.

### Testing

- Expanded test suite with security-focused tests for memory amplification and duplicate key detection.
- Added tests for `to_bytes_with_capacity`, `from_bytes_seed`, and other previously uncovered code paths.
- Improved benchmark suite with comprehensive type coverage and deserialization benchmarks.

## [v0.1.1] - 2020-12-11
- Renaming crate into "bcs".

## [v0.1.0] - 2020-11-17
- Initial release.

[Unreleased]: https://github.com/diem/bcs/compare/v0.1.1...HEAD
[v0.1.1]: https://github.com/diem/bcs/releases/tag/v0.1.1
[v0.1.0]: https://github.com/diem/bcs/releases/tag/v0.1.0
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ proptest-derive = "0.2.0"
[[bench]]
name = "bcs_bench"
harness = false

[lints.rust]
unsafe_code = "forbid"

[lints.clippy]
all = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
# These casts are intentional for binary serialization
cast_possible_truncation = "allow"
cast_possible_wrap = "allow"
cast_sign_loss = "allow"
Loading