-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from CoinFabrik/migrate-avoid-core-mem-forget…
…-documentation New avoid-core-mem-forget documentation
- Loading branch information
Showing
1 changed file
with
33 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
# Avoid core mem forget usage | ||
# Avoid core::mem::forget usage | ||
|
||
### What it does | ||
## Description | ||
|
||
Checks for `core::mem::forget` usage. | ||
- Category: `Best practices` | ||
- Severity: `Enhancement` | ||
- Detector: [`avoid-core-mem-forget`](https://github.com/CoinFabrik/scout-soroban/tree/main/detectors/avoid-core-mem-forget) | ||
- Test Cases: [`avoid-core-mem-forget-1`](https://github.com/CoinFabrik/scout-soroban/tree/main/test-cases/avoid-core-mem-forget/avoid-core-mem-forget-1) | ||
|
||
### Why is this bad? | ||
|
||
This is a bad practice because it can lead to memory leaks, resource leaks and logic errors. | ||
The `core::mem::forget` function is used to forget about a value without running its destructor. | ||
|
||
### Example | ||
## Why is this bad? | ||
|
||
```rust | ||
pub fn forget_something(n: WithoutCopy) -> u64 { | ||
core::mem::forget(n); | ||
0 | ||
} | ||
``` | ||
Using this function is a bad practice because this can lead to memory leaks, resource leaks and logic errors. | ||
|
||
## Issue example | ||
|
||
Use instead: | ||
Consider the following `Soroban` contract: | ||
|
||
```rust | ||
pub fn forget_something(n: WithoutCopy) -> u64 { | ||
let _ = n; | ||
0 | ||
} | ||
|
||
pub fn forget_something(n: WithoutCopy) -> u64 { | ||
core::mem::forget(n); | ||
0 | ||
} | ||
``` | ||
|
||
### Implementation | ||
The problem arises from the use of the `core::mem::forget` function. | ||
|
||
The code example can be found [here](https://github.com/CoinFabrik/scout-soroban/tree/main/test-cases/avoid-core-mem-forget/avoid-core-mem-forget-1/vulnerable-example). | ||
|
||
|
||
## Remediated example | ||
|
||
Use the pattern `let _ = n;` or the `.drop()` method instead of `core::mem::forget(n);`. | ||
|
||
## How is it detected? | ||
|
||
Checks for `core::mem::forget` usage. | ||
|
||
## References | ||
|
||
The detector's implementation can be found at [this link](https://github.com/CoinFabrik/scout-soroban/tree/main/detectors/avoid-core-mem-forget). | ||
- [Memory Management](https://docs.alephzero.org/aleph-zero/security-course-by-kudelski-security/ink-developers-security-guideline#memory-management) | ||
|