Skip to content

Commit aa96548

Browse files
committed
Address follow-up CodeRabbit findings
1 parent 6031ee8 commit aa96548

7 files changed

Lines changed: 202 additions & 75 deletions

File tree

‎crates/host_env/src/cert_store.rs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use std::io;
22

33
use schannel::{RawPointer, cert_context::ValidUses, cert_store::CertStore};
4-
use windows_sys::Win32::Security::Cryptography::{
5-
CERT_CONTEXT, CRL_CONTEXT, CertCloseStore, CertEnumCRLsInStore, CertOpenSystemStoreW,
6-
PKCS_7_ASN_ENCODING, X509_ASN_ENCODING,
4+
use windows_sys::Win32::{
5+
Foundation::{CRYPT_E_NOT_FOUND, GetLastError},
6+
Security::Cryptography::{
7+
CERT_CONTEXT, CRL_CONTEXT, CertCloseStore, CertEnumCRLsInStore, CertOpenSystemStoreW,
8+
PKCS_7_ASN_ENCODING, X509_ASN_ENCODING,
9+
},
710
};
811

912
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -39,10 +42,12 @@ pub struct CrlEntry {
3942
}
4043

4144
fn encoding_type(raw: u32) -> EncodingType {
42-
match raw {
43-
X509_ASN_ENCODING => EncodingType::X509Asn,
44-
PKCS_7_ASN_ENCODING => EncodingType::Pkcs7Asn,
45-
other => EncodingType::Other(other),
45+
if raw & X509_ASN_ENCODING != 0 {
46+
EncodingType::X509Asn
47+
} else if raw & PKCS_7_ASN_ENCODING != 0 {
48+
EncodingType::Pkcs7Asn
49+
} else {
50+
EncodingType::Other(raw)
4651
}
4752
}
4853

@@ -101,6 +106,13 @@ pub fn enum_crls(store_name: &str) -> io::Result<Vec<CrlEntry>> {
101106
loop {
102107
crl_context = unsafe { CertEnumCRLsInStore(store, crl_context) };
103108
if crl_context.is_null() {
109+
let err = unsafe { GetLastError() };
110+
if err != CRYPT_E_NOT_FOUND as u32 {
111+
unsafe {
112+
CertCloseStore(store, 0);
113+
}
114+
return Err(io::Error::from_raw_os_error(err as i32));
115+
}
104116
break;
105117
}
106118

‎crates/host_env/src/locale.rs‎

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ struct RawLconv {
2222
n_sep_by_space: libc::c_char,
2323
p_sign_posn: libc::c_char,
2424
n_sign_posn: libc::c_char,
25-
int_p_cs_precedes: libc::c_char,
26-
int_p_sep_by_space: libc::c_char,
27-
int_n_cs_precedes: libc::c_char,
28-
int_n_sep_by_space: libc::c_char,
29-
int_p_sign_posn: libc::c_char,
30-
int_n_sign_posn: libc::c_char,
3125
}
3226

3327
#[cfg(windows)]
@@ -113,13 +107,13 @@ pub fn strcoll(string1: &CStr, string2: &CStr) -> libc::c_int {
113107
unsafe { libc::strcoll(string1.as_ptr(), string2.as_ptr()) }
114108
}
115109

116-
pub fn strxfrm(string: &CStr, initial_len: usize) -> Vec<u8> {
117-
let mut buff = vec![0u8; initial_len];
118-
let n2 = unsafe { libc::strxfrm(buff.as_mut_ptr() as _, string.as_ptr(), initial_len) };
119-
buff = vec![0u8; n2 + 1];
110+
pub fn strxfrm(string: &CStr, _initial_len: usize) -> Vec<u8> {
111+
let len = unsafe { libc::strxfrm(ptr::null_mut(), string.as_ptr(), 0) };
112+
let mut buff = vec![0u8; len + 1];
120113
unsafe {
121-
libc::strxfrm(buff.as_mut_ptr() as _, string.as_ptr(), n2 + 1);
114+
libc::strxfrm(buff.as_mut_ptr() as _, string.as_ptr(), buff.len());
122115
}
116+
buff.truncate(len);
123117
buff
124118
}
125119

@@ -143,29 +137,19 @@ pub fn decode_ansi_bytes(bytes: &[u8]) -> Option<String> {
143137
use core::ptr;
144138
use windows_sys::Win32::Globalization::{CP_ACP, MultiByteToWideChar};
145139

146-
let len = unsafe {
147-
MultiByteToWideChar(
148-
CP_ACP,
149-
0,
150-
bytes.as_ptr(),
151-
bytes.len() as i32,
152-
ptr::null_mut(),
153-
0,
154-
)
155-
};
140+
if bytes.is_empty() {
141+
return Some(String::new());
142+
}
143+
let len_i32 = i32::try_from(bytes.len()).ok()?;
144+
145+
let len =
146+
unsafe { MultiByteToWideChar(CP_ACP, 0, bytes.as_ptr(), len_i32, ptr::null_mut(), 0) };
156147
if len <= 0 {
157148
return None;
158149
}
159150
let mut wide = vec![0u16; len as usize];
160151
unsafe {
161-
MultiByteToWideChar(
162-
CP_ACP,
163-
0,
164-
bytes.as_ptr(),
165-
bytes.len() as i32,
166-
wide.as_mut_ptr(),
167-
len,
168-
);
152+
MultiByteToWideChar(CP_ACP, 0, bytes.as_ptr(), len_i32, wide.as_mut_ptr(), len);
169153
}
170154
Some(String::from_utf16_lossy(&wide))
171155
}

‎crates/host_env/src/multiprocessing.rs‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use windows_sys::Win32::{
9090
CloseHandle, ERROR_TOO_MANY_POSTS, GetLastError, HANDLE, INVALID_HANDLE_VALUE, WAIT_FAILED,
9191
WAIT_OBJECT_0, WAIT_TIMEOUT,
9292
},
93-
Networking::WinSock::{SOCKET, closesocket, recv, send},
93+
Networking::WinSock::{SOCKET, WSAGetLastError, closesocket, recv, send},
9494
System::Threading::{
9595
CreateSemaphoreW, GetCurrentThreadId, INFINITE, ReleaseSemaphore, WaitForSingleObjectEx,
9696
},
@@ -126,10 +126,15 @@ impl SemHandle {
126126
return Err(SemError::from_errno(Errno::last()));
127127
}
128128
if unlink {
129-
unsafe {
130-
libc::sem_unlink(cname.as_ptr());
129+
if unsafe { libc::sem_unlink(cname.as_ptr()) } != 0 {
130+
let err = SemError::from_errno(Errno::last());
131+
unsafe {
132+
libc::sem_close(raw);
133+
}
134+
Err(err)
135+
} else {
136+
Ok((Self { raw }, None))
131137
}
132-
Ok((Self { raw }, None))
133138
} else {
134139
Ok((Self { raw }, Some(name.to_owned())))
135140
}
@@ -204,8 +209,8 @@ pub fn current_thread_id() -> u64 {
204209

205210
#[cfg(windows)]
206211
#[inline]
207-
pub fn current_thread_id() -> u32 {
208-
unsafe { GetCurrentThreadId() }
212+
pub fn current_thread_id() -> u64 {
213+
unsafe { GetCurrentThreadId() as u64 }
209214
}
210215

211216
#[cfg(windows)]
@@ -231,18 +236,21 @@ pub fn wait_timeout() -> u32 {
231236
pub fn close_socket(socket: SOCKET) -> io::Result<()> {
232237
let res = unsafe { closesocket(socket) };
233238
if res != 0 {
234-
Err(io::Error::last_os_error())
239+
Err(io::Error::from_raw_os_error(unsafe { WSAGetLastError() }))
235240
} else {
236241
Ok(())
237242
}
238243
}
239244

240245
#[cfg(windows)]
241246
pub fn recv_socket(socket: SOCKET, size: usize) -> io::Result<Vec<u8>> {
247+
let len = i32::try_from(size).map_err(|_| {
248+
io::Error::new(io::ErrorKind::InvalidInput, "socket receive size too large")
249+
})?;
242250
let mut buf = vec![0u8; size];
243-
let n_read = unsafe { recv(socket, buf.as_mut_ptr() as *mut _, size as i32, 0) };
251+
let n_read = unsafe { recv(socket, buf.as_mut_ptr() as *mut _, len, 0) };
244252
if n_read < 0 {
245-
Err(io::Error::last_os_error())
253+
Err(io::Error::from_raw_os_error(unsafe { WSAGetLastError() }))
246254
} else {
247255
buf.truncate(n_read as usize);
248256
Ok(buf)
@@ -251,9 +259,11 @@ pub fn recv_socket(socket: SOCKET, size: usize) -> io::Result<Vec<u8>> {
251259

252260
#[cfg(windows)]
253261
pub fn send_socket(socket: SOCKET, buf: &[u8]) -> io::Result<i32> {
254-
let ret = unsafe { send(socket, buf.as_ptr() as *const _, buf.len() as i32, 0) };
262+
let len = i32::try_from(buf.len())
263+
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "socket send buffer too large"))?;
264+
let ret = unsafe { send(socket, buf.as_ptr() as *const _, len, 0) };
255265
if ret < 0 {
256-
Err(io::Error::last_os_error())
266+
Err(io::Error::from_raw_os_error(unsafe { WSAGetLastError() }))
257267
} else {
258268
Ok(ret)
259269
}
@@ -382,14 +392,28 @@ pub fn gettimeofday() -> Result<libc::timeval, SemError> {
382392
#[cfg(unix)]
383393
pub fn deadline_from_timeout(timeout: f64) -> Result<libc::timespec, SemError> {
384394
let timeout = if timeout < 0.0 { 0.0 } else { timeout };
395+
if !timeout.is_finite() {
396+
return Err(SemError::InvalidInput);
397+
}
385398
let tv = gettimeofday()?;
386-
let sec = timeout as libc::c_long;
399+
let sec_f64 = timeout.floor();
400+
if sec_f64 > libc::time_t::MAX as f64 {
401+
return Err(SemError::InvalidInput);
402+
}
403+
let sec = sec_f64 as libc::time_t;
387404
let nsec = (1e9 * (timeout - sec as f64) + 0.5) as libc::c_long;
405+
let tv_nsec = (tv.tv_usec as libc::c_long)
406+
.checked_mul(1000)
407+
.and_then(|base| base.checked_add(nsec))
408+
.ok_or(SemError::InvalidInput)?;
388409
let mut deadline = libc::timespec {
389-
tv_sec: tv.tv_sec + sec as libc::time_t,
390-
tv_nsec: (tv.tv_usec as libc::c_long * 1000 + nsec) as _,
410+
tv_sec: tv.tv_sec.checked_add(sec).ok_or(SemError::InvalidInput)?,
411+
tv_nsec: tv_nsec as _,
391412
};
392-
deadline.tv_sec += (deadline.tv_nsec / 1_000_000_000) as libc::time_t;
413+
deadline.tv_sec = deadline
414+
.tv_sec
415+
.checked_add((deadline.tv_nsec / 1_000_000_000) as libc::time_t)
416+
.ok_or(SemError::InvalidInput)?;
393417
deadline.tv_nsec %= 1_000_000_000;
394418
Ok(deadline)
395419
}

0 commit comments

Comments
 (0)