Skip to content

Commit b10937f

Browse files
committed
fix: iterating stop at the first NULL argument
Do it like rust-lang/rust#106001
1 parent 9b686c6 commit b10937f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
ptr, slice,
1010
};
1111

12-
use log::{error, trace};
12+
use log::{error, trace, warn};
1313
use thiserror::Error;
1414

1515
#[derive(Error, Debug)]
@@ -60,17 +60,24 @@ unsafe fn from_addr(count: usize, ptr: *const *const c_char) -> Result<MemInfo,
6060
let mut saved: Vec<CString> = Vec::with_capacity(count);
6161
for i in 0..count {
6262
let cstr_ptr = *ptr.add(i);
63+
trace!(
64+
"string[{i}] ptr info: current ptr={:?}, point to={cstr_ptr:?}",
65+
ptr.add(i)
66+
);
67+
if cstr_ptr.is_null() {
68+
warn!("the string[{i}] is null, pls check");
69+
break;
70+
}
6371
let cstr_len = CStr::from_ptr(cstr_ptr).to_bytes_with_nul().len();
6472
saved.push(CStr::from_ptr(cstr_ptr).into());
6573
// Decide elsewhere whether to exclude nul.
6674
byte_len += cstr_len;
6775

68-
trace!("string[{i}] collect: recorded len={byte_len}, ptr={cstr_ptr:?}, string len={cstr_len}, next ptr={:?}",
76+
trace!(
77+
"string[{i}] collect: recorded len={byte_len}, start addr={cstr_ptr:?}, len={cstr_len}, end addr (with null)={:?}",
6978
cstr_ptr.add(cstr_len - 1)
7079
);
7180
if i == count - 1 {
72-
// It is assumed that arg/environ must never have an element
73-
// of length 0, otherwise unpredictable results would occur.
7481
// Perhaps it would be better to add 1 byte manually when
7582
// calculating the length.
7683
// Avoid overstepping the bounds.

0 commit comments

Comments
 (0)