Skip to content

Commit 52f0e2a

Browse files
committed
fix unsafty usages
1 parent 7b4028d commit 52f0e2a

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

lib/rust/scalgoproto.rs

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ pub unsafe fn to_enum<T: Enum>(v: u8) -> Option<T> {
9595
if v >= T::max_value() {
9696
None
9797
} else {
98-
let mut target: T = std::mem::zeroed();
99-
std::ptr::copy_nonoverlapping(&v as *const u8, &mut target as *mut T as *mut u8, 1);
100-
Some(target)
98+
unsafe {
99+
let mut target: T = std::mem::zeroed();
100+
std::ptr::copy_nonoverlapping(&v as *const u8, &mut target as *mut T as *mut u8, 1);
101+
Some(target)
102+
}
101103
}
102104
}
103105

@@ -106,20 +108,22 @@ pub unsafe fn to_enum<T: Enum>(v: u8) -> Option<T> {
106108
/// This method is safe to call if the length of v is sizeof(T)
107109
/// and the bit pattern described by v is a valid state for T
108110
pub unsafe fn to_pod<T: Pod>(v: &[u8]) -> T {
109-
let mut target: T = std::mem::zeroed();
110-
std::ptr::copy_nonoverlapping(
111-
v.as_ptr(),
112-
&mut target as *mut T as *mut u8,
113-
std::mem::size_of::<T>(),
114-
);
115-
target
111+
unsafe {
112+
let mut target: T = std::mem::zeroed();
113+
std::ptr::copy_nonoverlapping(
114+
v.as_ptr(),
115+
&mut target as *mut T as *mut u8,
116+
std::mem::size_of::<T>(),
117+
);
118+
target
119+
}
116120
}
117121

118122
/// # Safety
119123
///
120124
/// Should only be called if s is at least of size sizeof(S::B)
121125
pub unsafe fn to_struct<'a, S: StructIn<'a> + 'a>(s: &[u8]) -> S {
122-
S::new(&*(s as *const [u8] as *const S::B))
126+
unsafe { S::new(&*(s as *const [u8] as *const S::B)) }
123127
}
124128

125129
pub fn to_bool(v: u8) -> bool {
@@ -130,18 +134,22 @@ pub fn to_bool(v: u8) -> bool {
130134
///
131135
/// Should only be called when v.len() >= 6
132136
pub unsafe fn to_u48(v: &[u8]) -> u64 {
133-
let mut out: u64 = 0;
134-
std::ptr::copy_nonoverlapping(v.as_ptr(), &mut out as *mut u64 as *mut u8, 6);
135-
out
137+
unsafe {
138+
let mut out: u64 = 0;
139+
std::ptr::copy_nonoverlapping(v.as_ptr(), &mut out as *mut u64 as *mut u8, 6);
140+
out
141+
}
136142
}
137143

138144
/// # Safety
139145
///
140146
/// Should only be called when v.len() >= 6
141147
pub unsafe fn to_u48_usize(v: &[u8]) -> Result<usize> {
142-
match std::convert::TryFrom::try_from(to_u48(v)) {
143-
Ok(v) => Ok(v),
144-
Err(_) => Err(Error::Overflow()),
148+
unsafe {
149+
match std::convert::TryFrom::try_from(to_u48(v)) {
150+
Ok(v) => Ok(v),
151+
Err(_) => Err(Error::Overflow()),
152+
}
145153
}
146154
}
147155

@@ -1254,8 +1262,10 @@ impl<'a> ArenaSlice<'a> {
12541262
/// This is safe as long as o is within the arena, the pointer
12551263
/// will be valid util the arena, resizes or is dropped.
12561264
unsafe fn data(&self, o: usize) -> *mut u8 {
1257-
let data = &mut *self.arena.data.get();
1258-
data.as_mut_ptr().add(self.offset + o)
1265+
unsafe {
1266+
let data = &mut *self.arena.data.get();
1267+
data.as_mut_ptr().add(self.offset + o)
1268+
}
12591269
}
12601270

12611271
fn check_offset(&self, o: usize, size: usize) {
@@ -1276,11 +1286,13 @@ impl<'a> ArenaSlice<'a> {
12761286
///
12771287
/// This is safe if offset..offset+sizeof<T> is within the arena
12781288
pub unsafe fn set_pod_unsafe<T: Copy + std::fmt::Debug>(&mut self, offset: usize, value: &T) {
1279-
std::ptr::copy_nonoverlapping(
1280-
value as *const T as *const u8,
1281-
self.data(offset),
1282-
std::mem::size_of::<T>(),
1283-
);
1289+
unsafe {
1290+
std::ptr::copy_nonoverlapping(
1291+
value as *const T as *const u8,
1292+
self.data(offset),
1293+
std::mem::size_of::<T>(),
1294+
);
1295+
}
12841296
}
12851297

12861298
pub fn set_pod<T: Copy + std::fmt::Debug>(&mut self, offset: usize, value: &T) {
@@ -1292,11 +1304,13 @@ impl<'a> ArenaSlice<'a> {
12921304
///
12931305
/// This is safe if offset is within the arena and bit < 8
12941306
pub unsafe fn set_bit_unsafe(&mut self, offset: usize, bit: usize, value: bool) {
1295-
let d = self.data(offset);
1296-
if value {
1297-
*d |= 1 << bit;
1298-
} else {
1299-
*d &= !(1 << bit);
1307+
unsafe {
1308+
let d = self.data(offset);
1309+
if value {
1310+
*d |= 1 << bit;
1311+
} else {
1312+
*d &= !(1 << bit);
1313+
}
13001314
}
13011315
}
13021316

@@ -1310,7 +1324,7 @@ impl<'a> ArenaSlice<'a> {
13101324
///
13111325
/// This is safe if offset is within the arena
13121326
pub unsafe fn set_bool_unsafe(&mut self, offset: usize, value: bool) {
1313-
*self.data(offset) = if value { 1 } else { 0 }
1327+
unsafe { *self.data(offset) = if value { 1 } else { 0 } }
13141328
}
13151329

13161330
pub fn set_bool(&mut self, offset: usize, value: bool) {
@@ -1322,7 +1336,9 @@ impl<'a> ArenaSlice<'a> {
13221336
///
13231337
/// This is safe if offset,offset+6 is in the arena, and value is less than 1 << 42
13241338
pub unsafe fn set_u48_unsafe(&mut self, offset: usize, value: u64) {
1325-
std::ptr::copy_nonoverlapping(&value as *const u64 as *const u8, self.data(offset), 6);
1339+
unsafe {
1340+
std::ptr::copy_nonoverlapping(&value as *const u64 as *const u8, self.data(offset), 6);
1341+
}
13261342
}
13271343

13281344
pub fn set_u48(&mut self, offset: usize, value: u64) {
@@ -1335,11 +1351,15 @@ impl<'a> ArenaSlice<'a> {
13351351
///
13361352
/// This is safe if std::mem::size_of::<T>() == 1 and offset is within the arena
13371353
pub unsafe fn set_enum_unsafe<T: Enum>(&mut self, offset: usize, value: Option<T>) {
1338-
match value {
1339-
Some(vv) => {
1340-
std::ptr::copy_nonoverlapping(&vv as *const T as *const u8, self.data(offset), 1)
1354+
unsafe {
1355+
match value {
1356+
Some(vv) => std::ptr::copy_nonoverlapping(
1357+
&vv as *const T as *const u8,
1358+
self.data(offset),
1359+
1,
1360+
),
1361+
None => *self.data(offset) = 255,
13411362
}
1342-
None => *self.data(offset) = 255,
13431363
}
13441364
}
13451365

0 commit comments

Comments
 (0)