rust_types.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file rust_types.c
  5. * \brief This file is used for handling types returned from Rust to C.
  6. **/
  7. #include "or.h"
  8. #include "rust_types.h"
  9. #ifdef HAVE_RUST
  10. void free_rust_str(char *ret);
  11. /* Because Rust strings can only be freed from Rust, we first copy the string's
  12. * contents to a c pointer, and then free the Rust string.
  13. * This function can be extended to return a success/error value if needed.
  14. */
  15. void
  16. move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest)
  17. {
  18. if (!src) {
  19. log_warn(LD_BUG, "Received a null pointer from protover rust.");
  20. return;
  21. }
  22. if (!dest) {
  23. log_warn(LD_BUG, "Received a null pointer from caller to rust. "
  24. "This results in a memory leak due to not freeing the rust "
  25. "string that was meant to be copied..");
  26. return;
  27. }
  28. *dest = tor_strdup(src);
  29. free_rust_str(src);
  30. return;
  31. }
  32. #else
  33. /* When Rust is not enabled, this function should never be used. Log a warning
  34. * in the case that it is ever called when Rust is not enabled.
  35. */
  36. void
  37. move_rust_str_to_c_and_free(rust_str_ref_t src, char **dest)
  38. {
  39. (void) src;
  40. (void) dest;
  41. log_warn(LD_BUG, "Received a call to free a Rust string when we are "
  42. " not running with Rust enabled.");
  43. return;
  44. }
  45. #endif /* defined(HAVE_RUST) */