diff options
| author | Ralf Jung <post@ralfj.de> | 2024-04-29 17:33:35 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-04-29 17:33:35 +0200 |
| commit | 7afef08b6d01c4baabe4523f4b91ea2d1748f0e6 (patch) | |
| tree | 98e8a0d0ce842fc3dd92362552552b8b7561755b | |
| parent | 9bed19edc42fe7db30df8258d13598bfe7b1971b (diff) | |
| download | rust-7afef08b6d01c4baabe4523f4b91ea2d1748f0e6.tar.gz rust-7afef08b6d01c4baabe4523f4b91ea2d1748f0e6.zip | |
don't leak UnixEnvVars impl details into get_env_var
| -rw-r--r-- | src/tools/miri/src/shims/env.rs | 10 | ||||
| -rw-r--r-- | src/tools/miri/src/shims/unix/env.rs | 22 |
2 files changed, 19 insertions, 13 deletions
diff --git a/src/tools/miri/src/shims/env.rs b/src/tools/miri/src/shims/env.rs index b95abb484c8..695d1138756 100644 --- a/src/tools/miri/src/shims/env.rs +++ b/src/tools/miri/src/shims/env.rs @@ -106,15 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let this = self.eval_context_ref(); match &this.machine.env_vars { EnvVars::Uninit => return Ok(None), - EnvVars::Unix(vars) => { - let var_ptr = vars.get(this, name)?; - if let Some(ptr) = var_ptr { - let var = this.read_os_str_from_c_str(ptr)?; - Ok(Some(var.to_owned())) - } else { - Ok(None) - } - } + EnvVars::Unix(vars) => vars.get(this, name), EnvVars::Windows(vars) => vars.get(name), } } diff --git a/src/tools/miri/src/shims/unix/env.rs b/src/tools/miri/src/shims/unix/env.rs index 910e53260bf..9082d13da84 100644 --- a/src/tools/miri/src/shims/unix/env.rs +++ b/src/tools/miri/src/shims/unix/env.rs @@ -71,9 +71,7 @@ impl<'tcx> UnixEnvVars<'tcx> { self.environ.ptr() } - /// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete - /// with the reads of the environment, but returns an [`OsString`] instead of a pointer. - pub(crate) fn get<'mir>( + fn get_ptr<'mir>( &self, ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>, name: &OsStr, @@ -91,6 +89,22 @@ impl<'tcx> UnixEnvVars<'tcx> { )?; Ok(Some(var_ptr)) } + + /// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete + /// with the reads of the environment, but returns an [`OsString`] instead of a pointer. + pub(crate) fn get<'mir>( + &self, + ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>, + name: &OsStr, + ) -> InterpResult<'tcx, Option<OsString>> { + let var_ptr = self.get_ptr(ecx, name)?; + if let Some(ptr) = var_ptr { + let var = ecx.read_os_str_from_c_str(ptr)?; + Ok(Some(var.to_owned())) + } else { + Ok(None) + } + } } fn alloc_env_var<'mir, 'tcx>( @@ -137,7 +151,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let name_ptr = this.read_pointer(name_op)?; let name = this.read_os_str_from_c_str(name_ptr)?; - let var_ptr = this.machine.env_vars.unix().get(this, name)?; + let var_ptr = this.machine.env_vars.unix().get_ptr(this, name)?; Ok(var_ptr.unwrap_or_else(Pointer::null)) } |
