rust ffi buffer overflow
hi, do i have to null-terminate array when using rust ffi? i'm getting buffer overflows.
3 Replies
Based on the documentation, when calling FFI symbols with a buffer, the next argument must be the buffer’s length. However, in your code, you're not passing the buffer length to the
play
function. This could be the reason why you're experiencing buffer overflows.
Here's how you can modify your Rust and Deno code to include the buffer length:
Rust:
Deno:
In the Rust code, we're now accepting a length parameter and using std::slice::from_raw_parts
to create a slice from the raw parts. In the Deno code, we're passing the length of the buffer as the second argument to the play
function.
Please try this modification and see if it resolves your issue. 🦕C strings are null terminated, so indeed you need to null terminate manually on the JS side.
Alternatively, you can use the code suggested by the AI Helper and pass in the length of the buffer as well, creating a
str
which you can then turn into an owned String
if need be.thanks. i chose appending
\0
to parameter before encoding.