Do not pass dangling pointers to C++

This is the reverse direction of cl/539924021.

PiperOrigin-RevId: 540812123
pull/13085/head
Marcel Hlopko 1 year ago committed by Copybara-Service
parent 46e0b15e7c
commit f450ce2868
  1. 10
      src/google/protobuf/compiler/rust/accessors/singular_bytes.cc

@ -61,13 +61,17 @@ class SingularBytes final : public AccessorGenerator {
return None; return None;
} }
unsafe { unsafe {
let val = $getter_thunk$(self.msg); Some($getter_thunk$(self.msg).as_ref())
Some($std$::slice::from_raw_parts(val.ptr, val.len))
} }
} }
pub fn $field$_set(&mut self, val: Option<&[u8]>) { pub fn $field$_set(&mut self, val: Option<&[u8]>) {
match val { match val {
Some(val) => unsafe { $setter_thunk$(self.msg, val.as_ptr(), val.len()) }, Some(val) =>
if val.len() == 0 {
unsafe { $setter_thunk$(self.msg, $std$::ptr::null(), 0) }
} else {
unsafe { $setter_thunk$(self.msg, val.as_ptr(), val.len()) }
},
None => unsafe { $clearer_thunk$(self.msg) }, None => unsafe { $clearer_thunk$(self.msg) },
} }
} }

Loading…
Cancel
Save