tor_allocate.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. use libc::{c_char, c_void};
  2. use std::{ptr, slice, mem};
  3. #[cfg(not(test))]
  4. extern "C" {
  5. fn tor_malloc_ ( size: usize) -> *mut c_void;
  6. }
  7. // Defined only for tests, used for testing purposes, so that we don't need
  8. // to link to tor C files. Uses the system allocator
  9. #[cfg(test)]
  10. extern "C" fn tor_malloc_ ( size: usize) -> *mut c_void {
  11. use libc::malloc;
  12. unsafe { malloc(size) }
  13. }
  14. /// Allocate memory using tor_malloc_ and copy an existing string into the
  15. /// allocated buffer, returning a pointer that can later be called in C.
  16. ///
  17. /// # Inputs
  18. ///
  19. /// * `src`, a reference to a String.
  20. ///
  21. /// # Returns
  22. ///
  23. /// A `*mut c_char` that should be freed by tor_free in C
  24. ///
  25. /// Allow unused unsafe as at compile-time, we get warnings that unsafe is not
  26. /// needed even though this calls tor_malloc in C.
  27. ///
  28. #[allow(unused_unsafe)]
  29. pub fn allocate_and_copy_string(src: &String) -> *mut c_char {
  30. let bytes: &[u8] = src.as_bytes();
  31. let size = mem::size_of_val::<[u8]>(bytes);
  32. let size_one_byte = mem::size_of::<u8>();
  33. // handle integer overflow when adding one to the calculated length
  34. let size_with_null_byte = match size.checked_add(size_one_byte) {
  35. Some(n) => n,
  36. None => return ptr::null_mut(),
  37. };
  38. let dest = unsafe { tor_malloc_(size_with_null_byte) as *mut u8 };
  39. if dest.is_null() {
  40. return ptr::null_mut();
  41. }
  42. unsafe { ptr::copy_nonoverlapping(bytes.as_ptr(), dest, size) };
  43. // set the last byte as null, using the ability to index into a slice
  44. // rather than doing pointer arithmatic
  45. let slice = unsafe { slice::from_raw_parts_mut(dest, size_with_null_byte)};
  46. slice[size] = 0; // add a null terminator
  47. dest as *mut c_char
  48. }
  49. #[cfg(test)]
  50. mod test {
  51. #[test]
  52. fn test_allocate_and_copy_string_with_empty() {
  53. use std::ffi::CStr;
  54. use libc::{free, c_void};
  55. use tor_allocate::allocate_and_copy_string;
  56. let empty = String::new();
  57. let allocated_empty = allocate_and_copy_string(&empty);
  58. let allocated_empty_rust = unsafe {
  59. CStr::from_ptr(allocated_empty).to_str().unwrap()
  60. };
  61. assert_eq!("", allocated_empty_rust);
  62. unsafe { free(allocated_empty as *mut c_void) };
  63. }
  64. #[test]
  65. fn test_allocate_and_copy_string_with_not_empty_string() {
  66. use std::ffi::CStr;
  67. use libc::{free, c_void};
  68. use tor_allocate::allocate_and_copy_string;
  69. let empty = String::from("foo bar biz");
  70. let allocated_empty = allocate_and_copy_string(&empty);
  71. let allocated_empty_rust = unsafe {
  72. CStr::from_ptr(allocated_empty).to_str().unwrap()
  73. };
  74. assert_eq!("foo bar biz", allocated_empty_rust);
  75. unsafe { free(allocated_empty as *mut c_void) };
  76. }
  77. }