Skip to content

Commit

Permalink
fix: Another approach to fixing buffer issue
Browse files Browse the repository at this point in the history
  • Loading branch information
benaclejames committed Apr 4, 2024
1 parent 58bc47f commit 9163766
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,24 +321,27 @@ pub extern "C" fn create_osc_bundle(buf: *mut c_uchar, messages: *const OscMessa
ix += 8;

// Now we need to write the messages
let mut temp_buffer: [c_uchar; 4096] = [0; 4096];
for msg in messages.iter().skip(*messages_index as usize) {
// We need to calculate the length of the string and pad it to a multiple of 4 to ensure alignment
// then add another 4 bytes for the length of the message
// If adding it would go over the buffer size, return
// Use the existing function to write the message to the buffer
let address = unsafe { CStr::from_ptr(msg.address).to_str() }.unwrap();
let length = address.len() + 1;
let padded_length = if length % 4 == 0 { length } else { length + 4 - (length % 4) };
if ix + padded_length + 4 >= 4096 {
return ix as i32;

//let length = create_osc_message(unsafe { buf.as_mut_ptr().add(ix + 4) }, msg);
let length = create_osc_message(temp_buffer.as_mut_ptr(), msg);
if length + ix >= 4096 {
break;
}

let length = create_osc_message(unsafe { buf.as_mut_ptr().add(ix + 4) }, msg);
// Write the length of the message to the buffer. Ensure we use 4 bytes
let bytes: [u8; 4] = (length as i32).to_be_bytes();

buf[ix..ix + 4].copy_from_slice(&bytes);
ix += length + 4;
ix += 4;

buf[ix..ix + length].copy_from_slice(&temp_buffer[0..length]);
ix += length;

// Update the message index after each iteration
*messages_index += 1;
Expand Down

0 comments on commit 9163766

Please sign in to comment.