Skip to content

Commit

Permalink
fix leak and mut modifier
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Oct 5, 2024
1 parent d2f6678 commit f1a6c86
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions bindings/c/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ pub struct opendal_entry {
}

impl opendal_entry {
fn deref(&self) -> &mut core::Entry {
fn deref(&self) -> &core::Entry {
// Safety: the inner should never be null once constructed
// The use-after-free is undefined behavior
unsafe { &mut *(self.inner as *mut core::Entry) }
unsafe { &*(self.inner as *mut core::Entry) }
}
}

Expand Down Expand Up @@ -77,8 +77,8 @@ impl opendal_entry {
#[no_mangle]
pub unsafe extern "C" fn opendal_entry_free(ptr: *mut opendal_entry) {
if !ptr.is_null() {
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = unsafe { Box::from_raw(ptr) };
let _ = Box::from_raw((*ptr).inner as *mut core::Entry);
let _ = Box::from_raw(ptr);
}
}
}
8 changes: 4 additions & 4 deletions bindings/c/src/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct opendal_lister {
}

impl opendal_lister {
fn deref(&self) -> &mut core::BlockingLister {
fn deref_mut(&self) -> &mut core::BlockingLister {
// Safety: the inner should never be null once constructed
// The use-after-free is undefined behavior
unsafe { &mut *(self.inner as *mut core::BlockingLister) }
Expand All @@ -56,7 +56,7 @@ impl opendal_lister {
/// @see opendal_operator_list()
#[no_mangle]
pub unsafe extern "C" fn opendal_lister_next(&self) -> opendal_result_lister_next {
let e = self.deref().next();
let e = self.deref_mut().next();
if e.is_none() {
return opendal_result_lister_next {
entry: std::ptr::null_mut(),
Expand All @@ -83,8 +83,8 @@ impl opendal_lister {
#[no_mangle]
pub unsafe extern "C" fn opendal_lister_free(ptr: *mut opendal_lister) {
if !ptr.is_null() {
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = unsafe { Box::from_raw(ptr) };
let _ = Box::from_raw((*ptr).inner as *mut core::BlockingLister);
let _ = Box::from_raw(ptr);
}
}
}
8 changes: 4 additions & 4 deletions bindings/c/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub struct opendal_metadata {
}

impl opendal_metadata {
fn deref(&self) -> &mut core::Metadata {
fn deref(&self) -> &core::Metadata {
// Safety: the inner should never be null once constructed
// The use-after-free is undefined behavior
unsafe { &mut *(self.inner as *mut core::Metadata) }
unsafe { &*(self.inner as *mut core::Metadata) }
}
}

Expand All @@ -55,8 +55,8 @@ impl opendal_metadata {
#[no_mangle]
pub unsafe extern "C" fn opendal_metadata_free(ptr: *mut opendal_metadata) {
if !ptr.is_null() {
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = unsafe { Box::from_raw(ptr) };
let _ = Box::from_raw((*ptr).inner as *mut core::Metadata);
let _ = Box::from_raw(ptr);
}
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/c/src/operator_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ impl opendal_operator_info {
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_info_free(ptr: *mut Self) {
if !ptr.is_null() {
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = unsafe { Box::from_raw(ptr) };
let _ = Box::from_raw((*ptr).inner as *mut core::OperatorInfo);
let _ = Box::from_raw(ptr);
}
}

Expand Down
8 changes: 4 additions & 4 deletions bindings/c/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct opendal_reader {
}

impl opendal_reader {
fn deref(&self) -> &mut core::StdReader {
fn deref_mut(&self) -> &mut core::StdReader {
// Safety: the inner should never be null once constructed
// The use-after-free is undefined behavior
unsafe { &mut *(self.inner as *mut core::StdReader) }
Expand All @@ -58,7 +58,7 @@ impl opendal_reader {
}

let buf = unsafe { std::slice::from_raw_parts_mut(buf, len) };
match self.deref().read(buf) {
match self.deref_mut().read(buf) {
Ok(n) => opendal_result_reader_read {
size: n,
error: std::ptr::null_mut(),
Expand All @@ -77,8 +77,8 @@ impl opendal_reader {
#[no_mangle]
pub unsafe extern "C" fn opendal_reader_free(ptr: *mut opendal_reader) {
if !ptr.is_null() {
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = unsafe { Box::from_raw(ptr) };
let _ = Box::from_raw((*ptr).inner as *mut core::StdReader);
let _ = Box::from_raw(ptr);
}
}
}
4 changes: 2 additions & 2 deletions bindings/c/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ impl opendal_operator_options {
#[no_mangle]
pub unsafe extern "C" fn opendal_operator_options_free(ptr: *mut opendal_operator_options) {
if !ptr.is_null() {
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = unsafe { Box::from_raw(ptr) };
let _ = Box::from_raw((*ptr).inner as *mut HashMap<String, String>);
let _ = Box::from_raw(ptr);
}
}
}
8 changes: 4 additions & 4 deletions bindings/c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct opendal_writer {
}

impl opendal_writer {
fn deref(&self) -> &mut core::BlockingWriter {
fn deref_mut(&self) -> &mut core::BlockingWriter {
// Safety: the inner should never be null once constructed
// The use-after-free is undefined behavior
unsafe { &mut *(self.inner as *mut core::BlockingWriter) }
Expand All @@ -50,7 +50,7 @@ impl opendal_writer {
bytes: opendal_bytes,
) -> opendal_result_writer_write {
let size = bytes.len;
match self.deref().write(bytes) {
match self.deref_mut().write(bytes) {
Ok(()) => opendal_result_writer_write {
size,
error: std::ptr::null_mut(),
Expand All @@ -70,8 +70,8 @@ impl opendal_writer {
#[no_mangle]
pub unsafe extern "C" fn opendal_writer_free(ptr: *mut opendal_writer) {
if !ptr.is_null() {
let _ = (&*ptr).deref().close();
let _ = unsafe { Box::from_raw((*ptr).inner) };
let _ = (&*ptr).deref_mut().close();
let _ = unsafe { Box::from_raw((*ptr).inner as *mut core::BlockingWriter) };
let _ = unsafe { Box::from_raw(ptr) };
}
}
Expand Down

0 comments on commit f1a6c86

Please sign in to comment.