fix (libzkp): free Rust CString by from_raw (potential memory leak) (#539)

* Free Rust CString by `from_raw`.

* Update params version.
This commit is contained in:
Steven 2023-10-25 15:04:13 +08:00 committed by GitHub
parent fcb3e1768d
commit adde9ce118
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 6 deletions

View file

@ -24,7 +24,7 @@ import (
const ( const (
VersionMajor = 5 // Major version component of the current release VersionMajor = 5 // Major version component of the current release
VersionMinor = 0 // Minor version component of the current release VersionMinor = 0 // Minor version component of the current release
VersionPatch = 1 // Patch version component of the current release VersionPatch = 2 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string VersionMeta = "mainnet" // Version metadata to append to the version string
) )

View file

@ -79,7 +79,7 @@ func (ccc *CircuitCapacityChecker) ApplyTransaction(traces *types.BlockTrace) (*
log.Debug("start to check circuit capacity for tx", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash) log.Debug("start to check circuit capacity for tx", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
rawResult := C.apply_tx(C.uint64_t(ccc.ID), tracesStr) rawResult := C.apply_tx(C.uint64_t(ccc.ID), tracesStr)
defer func() { defer func() {
C.free(unsafe.Pointer(rawResult)) C.free_c_chars(rawResult)
}() }()
log.Debug("check circuit capacity for tx done", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash) log.Debug("check circuit capacity for tx done", "id", ccc.ID, "TxHash", traces.Transactions[0].TxHash)
@ -125,7 +125,7 @@ func (ccc *CircuitCapacityChecker) ApplyBlock(traces *types.BlockTrace) (*types.
log.Debug("start to check circuit capacity for block", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash()) log.Debug("start to check circuit capacity for block", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())
rawResult := C.apply_block(C.uint64_t(ccc.ID), tracesStr) rawResult := C.apply_block(C.uint64_t(ccc.ID), tracesStr)
defer func() { defer func() {
C.free(unsafe.Pointer(rawResult)) C.free_c_chars(rawResult)
}() }()
log.Debug("check circuit capacity for block done", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash()) log.Debug("check circuit capacity for block done", "id", ccc.ID, "blockNumber", traces.Header.Number, "blockHash", traces.Header.Hash())
@ -157,7 +157,7 @@ func (ccc *CircuitCapacityChecker) CheckTxNum(expected int) (bool, uint64, error
log.Debug("ccc get_tx_num start", "id", ccc.ID) log.Debug("ccc get_tx_num start", "id", ccc.ID)
rawResult := C.get_tx_num(C.uint64_t(ccc.ID)) rawResult := C.get_tx_num(C.uint64_t(ccc.ID))
defer func() { defer func() {
C.free(unsafe.Pointer(rawResult)) C.free_c_chars(rawResult)
}() }()
log.Debug("ccc get_tx_num end", "id", ccc.ID) log.Debug("ccc get_tx_num end", "id", ccc.ID)
@ -180,7 +180,7 @@ func (ccc *CircuitCapacityChecker) SetLightMode(lightMode bool) error {
log.Debug("ccc set_light_mode start", "id", ccc.ID) log.Debug("ccc set_light_mode start", "id", ccc.ID)
rawResult := C.set_light_mode(C.uint64_t(ccc.ID), C.bool(lightMode)) rawResult := C.set_light_mode(C.uint64_t(ccc.ID), C.bool(lightMode))
defer func() { defer func() {
C.free(unsafe.Pointer(rawResult)) C.free_c_chars(rawResult)
}() }()
log.Debug("ccc set_light_mode end", "id", ccc.ID) log.Debug("ccc set_light_mode end", "id", ccc.ID)

View file

@ -8,3 +8,4 @@ char* apply_tx(uint64_t id, char *tx_traces);
char* apply_block(uint64_t id, char *block_trace); char* apply_block(uint64_t id, char *block_trace);
char* get_tx_num(uint64_t id); char* get_tx_num(uint64_t id);
char* set_light_mode(uint64_t id, bool light_mode); char* set_light_mode(uint64_t id, bool light_mode);
void free_c_chars(char* ptr);

View file

@ -259,11 +259,22 @@ pub mod checker {
} }
} }
pub(crate) mod utils { pub mod utils {
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::os::raw::c_char; use std::os::raw::c_char;
use std::str::Utf8Error; use std::str::Utf8Error;
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn free_c_chars(ptr: *mut c_char) {
if ptr.is_null() {
log::warn!("Try to free an empty pointer!");
return;
}
let _ = CString::from_raw(ptr);
}
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) fn c_char_to_str(c: *const c_char) -> Result<&'static str, Utf8Error> { pub(crate) fn c_char_to_str(c: *const c_char) -> Result<&'static str, Utf8Error> {
let cstr = unsafe { CStr::from_ptr(c) }; let cstr = unsafe { CStr::from_ptr(c) };