Skip to content

Fix a portability problem in rosidl_runtime_rs::String #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions rosidl_runtime_rs/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub struct StringExceedsBoundsError {

// There is a lot of redundancy between String and WString, which this macro aims to reduce.
macro_rules! string_impl {
($string:ty, $char_type:ty, $string_conversion_func:ident, $init:ident, $fini:ident, $assignn:ident, $sequence_init:ident, $sequence_fini:ident, $sequence_copy:ident) => {
($string:ty, $char_type:ty, $unsigned_char_type:ty, $string_conversion_func:ident, $init:ident, $fini:ident, $assignn:ident, $sequence_init:ident, $sequence_fini:ident, $sequence_copy:ident) => {
#[link(name = "rosidl_runtime_c")]
extern "C" {
fn $init(s: *mut $string) -> bool;
Expand Down Expand Up @@ -159,21 +159,26 @@ macro_rules! string_impl {
fn deref(&self) -> &Self::Target {
// SAFETY: self.data points to self.size consecutive, initialized elements and
// isn't modified externally.
unsafe { std::slice::from_raw_parts(self.data as *const $char_type, self.size) }
unsafe { std::slice::from_raw_parts(self.data, self.size) }
}
}

impl DerefMut for $string {
fn deref_mut(&mut self) -> &mut Self::Target {
// SAFETY: self.data points to self.size consecutive, initialized elements and
// isn't modified externally.
unsafe { std::slice::from_raw_parts_mut(self.data as *mut $char_type, self.size) }
unsafe { std::slice::from_raw_parts_mut(self.data, self.size) }
}
}

impl Display for $string {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let converted = std::string::String::$string_conversion_func(self.deref());
// SAFETY: Same as deref, but with an additional cast to the unsigned type.
// See also https://users.rust-lang.org/t/how-to-convert-i8-to-u8/16308/11
let u8_slice = unsafe {
std::slice::from_raw_parts(self.data as *mut $unsigned_char_type, self.size)
};
let converted = std::string::String::$string_conversion_func(u8_slice);
Display::fmt(&converted, f)
}
}
Expand Down Expand Up @@ -238,6 +243,7 @@ macro_rules! string_impl {

string_impl!(
String,
libc::c_char,
u8,
from_utf8_lossy,
rosidl_runtime_c__String__init,
Expand All @@ -250,6 +256,7 @@ string_impl!(
string_impl!(
WString,
libc::c_ushort,
u16,
from_utf16_lossy,
rosidl_runtime_c__U16String__init,
rosidl_runtime_c__U16String__fini,
Expand Down Expand Up @@ -321,7 +328,7 @@ impl<const N: usize> Debug for BoundedString<N> {
}

impl<const N: usize> Deref for BoundedString<N> {
type Target = [u8];
type Target = [libc::c_char];
fn deref(&self) -> &Self::Target {
self.inner.deref()
}
Expand Down
9 changes: 6 additions & 3 deletions rosidl_runtime_rs/src/string/serde.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use std::ops::Deref;

use super::{BoundedString, BoundedWString, String, WString};

Expand All @@ -18,7 +17,9 @@ impl Serialize for String {
S: Serializer,
{
// Not particularly efficient
let s = std::string::String::from_utf8_lossy(self.deref());
// SAFETY: See the Display implementation.
let u8_slice = unsafe { std::slice::from_raw_parts(self.data as *mut u8, self.size) };
let s = std::string::String::from_utf8_lossy(u8_slice);
serializer.serialize_str(&s)
}
}
Expand All @@ -38,7 +39,9 @@ impl Serialize for WString {
S: Serializer,
{
// Not particularly efficient
let s = std::string::String::from_utf16_lossy(self.deref());
// SAFETY: See the Display implementation.
let u16_slice = unsafe { std::slice::from_raw_parts(self.data as *mut u16, self.size) };
let s = std::string::String::from_utf16_lossy(u16_slice);
serializer.serialize_str(&s)
}
}
Expand Down