Is it safe to free data in C code that was malloc'd in Rust code?
⚓ Rust 📅 2025-11-06 👤 surdeus 👁️ 9Say I have a Rust function meant to be called from C code:
#[unsafe(no_mangle)]
extern "C" fn foo() -> *mut u8 {
unsafe { libc::malloc(42) }
}
Can I free the return value of this function in C code with:
#include <stdlib.h>
int main(void) {
uint8_t* x = foo();
free(x);
return 0;
}
Are there any dangerous assumptions being made here using free to free the return value of foo?
2 posts - 2 participants
🏷️ Rust_feed