1 unstable release
0.1.0 | Jan 10, 2024 |
---|
#24 in #inspector
9KB
123 lines
Rust Panic Free Analyzer
Overview
Rust Panic Free Analyzer is an audit tool designed to scan your Rust crate or workspace. Its primary function is to identify potential panic points in your codebase, leading you in developing binaries and libraries that are as close to "Panic Free" as possible.
âšī¸ As of now, it only currently searches the crates that you develop, and not the dependencies of your crates.
How does it work?
Key Identification Patterns
The tool searches for usage of several key patterns in Rust code that are often associated with panic points. These include:
panic!
: Direct calls to thepanic!
macro, which causes the program to terminate immediately and provide an error message.unwrap
: Calls to the.unwrap()
method, often used onOption
orResult
types, which will cause a panic if the value isNone
orErr
.expect
: Similar tounwrap
, but allows specifying a custom error message.Array Indexing
: Direct indexing into arrays (e.g., arr[index]) without bounds checking, which can panic if the index is out of bounds. (A safer indexing method is.get()
)unreachable!
: Indicates code that should never be reached; panics if executed.todo!
andunimplemented!
: Macros indicating incomplete or unimplemented code, which will panic if reached.
Installation
To start using it, you need to install it first.
cargo install panic-free-analyzer
Usage:
After installation, you can run the analyzer on your crate or entire workspace. Use the following command:
cargo panic-analyzer
If you wish to exclude specific crates from your workspace during the analysis, set the `IGNORED_CRATES`` environment variable. Pass the names of the crates you want to exclude, separated by commas:
IGNORED_CRATES=tests,benches cargo panic-analyzer
Audit Results Example đ
Below is an example of an audit result generated by the Rust Panic Free Analyzer:
đ¨ Rust Panic Audit: 141 Potential Panic Points Detected đ¨
Crate: vrl
đ Total Usages: 37
- đ
expect
usages: 1 - đ
unwrap
usages: 32 - đ¨
panic
usages: 1 - đĸ
array_index
usages: 3
Crate: jwt_auth
đ Total Usages: 31
- đ
unwrap
usages: 29 - đĸ
array_index
usages: 2
Crate: config
đ Total Usages: 14
- đ¨
panic
usages: 3 - đ
expect
usages: 3 - đ
unwrap
usages: 8
đ Expected Annotations
Crate: common
đ Total Expected Usages: 1
- Reason: "we need this"
- Code:
panic!("Exited process!")
- Location:
./libs/common/src/lib.rs:18
Dependencies
~3â11MB
~113K SLoC