Skip to content

Commit

Permalink
Merge pull request #54 from dadahsueh/master
Browse files Browse the repository at this point in the history
Fix unconditional removal of last byte #53
  • Loading branch information
ethangreen-dev authored Jun 30, 2024
2 parents 0a324f1 + c859f6a commit 50ff79f
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion crates/lovely-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ impl Lovely {
return (self.loadbuffer)(state, buf_ptr, size, name_ptr);
}

// Prepare buffer for patching (Check and remove the last byte if it is a null terminator)
let last_byte = *buf_ptr.add(size - 1);
let actual_size = if last_byte == 0 { size - 1 } else { size };

// Convert the buffer from cstr ptr, to byte slice, to utf8 str.
let buf = slice::from_raw_parts(buf_ptr, (size - 1) as _);
let buf = slice::from_raw_parts(buf_ptr, actual_size as _);
let buf_str = CString::new(buf)
.unwrap_or_else(|e| panic!("The byte buffer '{buf:?}' for target {name} contains a non-terminating null char: {e:?}"));
let buf_str = buf_str.to_str()
Expand Down

0 comments on commit 50ff79f

Please sign in to comment.