fix: add suggestions of @volcano0dr.
* Add early exit in the buffer copying part of readv and preadv64
* Use result.try_into().unwrap_or(0) instead of result.try_into().unwrap()
diff --git a/sgx_libc/src/linux/x86_64/ocall.rs b/sgx_libc/src/linux/x86_64/ocall.rs
index be2d9d4..205e6c4 100644
--- a/sgx_libc/src/linux/x86_64/ocall.rs
+++ b/sgx_libc/src/linux/x86_64/ocall.rs
@@ -1374,7 +1374,7 @@
}
if result != -1 {
- ptr::copy_nonoverlapping(tmp_buf as *const u8, buf as *mut u8, cmp::min(count, result.try_into().unwrap()));
+ ptr::copy_nonoverlapping(tmp_buf as *const u8, buf as *mut u8, cmp::min(count, result.try_into().unwrap_or(0)));
}
if count <= MAX_OCALL_ALLOC_SIZE {
sgx_ocfree();
@@ -1425,7 +1425,7 @@
}
if result != -1 {
- ptr::copy_nonoverlapping(tmp_buf as *const u8, buf as *mut u8, cmp::min(count, result.try_into().unwrap()));
+ ptr::copy_nonoverlapping(tmp_buf as *const u8, buf as *mut u8, cmp::min(count, result.try_into().unwrap_or(0)));
}
if count <= MAX_OCALL_ALLOC_SIZE {
sgx_ocfree();
@@ -1490,8 +1490,11 @@
}
if result != -1 {
- let mut remaining_bytes : usize = result.try_into().unwrap();
+ let mut remaining_bytes : usize = result.try_into().unwrap_or(0);
for i in 0..v.len() {
+ if remaining_bytes == 0 {
+ break;
+ }
// Here, we only copy the remaining bytes if there are less than the iov_len.
// Otherwise, the default 0s are copied into the buffer and overwrite data that should not be overwritten.
ptr::copy_nonoverlapping(
@@ -1562,8 +1565,11 @@
}
if result != -1 {
- let mut remaining_bytes : usize = result.try_into().unwrap();
+ let mut remaining_bytes : usize = result.try_into().unwrap_or(0);
for i in 0..v.len() {
+ if remaining_bytes == 0 {
+ break;
+ }
ptr::copy_nonoverlapping(tmpiovec[i].iov_base as *const u8, v[i].iov_base as *mut u8, cmp::min(v[i].iov_len, remaining_bytes));
remaining_bytes = remaining_bytes.saturating_sub(v[i].iov_len);
}