Skip to main content

Avoid core::mem::forget usage


The core::mem::forget function usage is a bad practice.

Exploit Scenario

Consider the following ink! contract:

pub fn forget_value(&mut self) {
let forgotten_value = self.value;
self.value = false;

The problem arises from the use of the core::mem::forget function. This function is used to forget about a value without running its destructor. This is a bad practice because it can lead to memory leaks, resource leaks and logic errors.

The vulnerable code example can be found here.


Use the pattern let _ = forgotten_value; or the .drop() method instead of core::mem::forget(forgotten_value);.