Skip to content

Commit

Permalink
Do not pass dangling pointers to C++
Browse files Browse the repository at this point in the history
This is the reverse direction of cl/539924021.

PiperOrigin-RevId: 540812123
  • Loading branch information
hlopko authored and copybara-github committed Jun 16, 2023
1 parent 46e0b15 commit f450ce2
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/google/protobuf/compiler/rust/accessors/singular_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ class SingularBytes final : public AccessorGenerator {
return None;
}
unsafe {
let val = $getter_thunk$(self.msg);
Some($std$::slice::from_raw_parts(val.ptr, val.len))
Some($getter_thunk$(self.msg).as_ref())
}
}
pub fn $field$_set(&mut self, val: Option<&[u8]>) {
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) },
}
}
Expand Down

0 comments on commit f450ce2

Please sign in to comment.