tor_allocate.rs 2.8 KB

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