Skip to content

Commit 2925226

Browse files
committed
Improve timezone handling in 'localtime_r()' using 'allocate_bytes()'
Signed-off-by: shamb0 <[email protected]>
1 parent 6f0a78a commit 2925226

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/shims/time.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
183183
if !matches!(&*this.tcx.sess.target.os, "solaris" | "illumos") {
184184
// tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
185185
// This may not be consistent with libc::localtime_r's result.
186-
const TZ_MAX_LEN: u64 = 6; // 5 chars max + null terminator
187-
static TIMEZONE_PTR: OnceLock<Pointer> = OnceLock::new();
188186

189187
let offset_in_seconds = dt.offset().fix().local_minus_utc();
190188
let tm_gmtoff = offset_in_seconds;
@@ -201,21 +199,17 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
201199
write!(tm_zone, "{:02}", offset_min).unwrap();
202200
}
203201

204-
// Ensure string deduplication by allocating the buffer only once,
205-
// even if the function is called multiple times.
206-
let tm_zone_ptr = TIMEZONE_PTR.get_or_init(|| {
207-
let arg_type = Ty::new_array(this.tcx.tcx, this.tcx.types.u8, TZ_MAX_LEN);
208-
let arg_place = this
209-
.allocate(this.layout_of(arg_type).unwrap(), MiriMemoryKind::Machine.into())
210-
.expect("timezone buffer allocation failed");
211-
arg_place.ptr()
212-
});
213-
214-
// Write the `tm_zone` string into the allocated buffer.
215-
let (written, _) =
216-
this.write_os_str_to_c_str(&OsString::from(tm_zone), *tm_zone_ptr, TZ_MAX_LEN)?;
217-
assert!(written);
218-
202+
// Add null terminator for C string compatibility
203+
tm_zone.push('\0');
204+
205+
// Deduplicate and allocate the string.
206+
let tm_zone_ptr = this.allocate_bytes(
207+
&tm_zone.as_bytes(),
208+
Align::ONE,
209+
MiriMemoryKind::Machine.into(),
210+
Mutability::Not,
211+
)?;
212+
219213
// Write the timezone pointer and offset into the result structure.
220214
this.write_pointer(*tm_zone_ptr, &this.project_field_named(&result, "tm_zone")?)?;
221215
this.write_int_fields_named(&[("tm_gmtoff", tm_gmtoff.into())], &result)?;

0 commit comments

Comments
 (0)