Skip to content

Commit 12040aa

Browse files
committed
Add comment explaining why buffer isn't overflowed
1 parent 8368d4f commit 12040aa

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/shims/env.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
133133
Ok(cwd) => {
134134
// It is not clear what happens with non-utf8 paths here
135135
let mut bytes = cwd.display().to_string().into_bytes();
136-
// If the buffer is smaller or equal than the path, we return null.
136+
// If `size` is smaller or equal than the `bytes.len()`, writing `bytes` using the
137+
// `buf` pointer would cause an overflow, the desired behavior in this case is to
138+
// return null.
137139
if (bytes.len() as u64) < size {
138140
// We add a `/0` terminator
139141
bytes.push(0);
140-
// This is ok because the buffer is larger than the path with the null terminator.
142+
// This is ok because the buffer was strictly larger than `bytes`, so after
143+
// adding the null terminator, the buffer size is larger or equal to
144+
// `bytes.len()`, meaning that `bytes` actually fit inside tbe buffer.
141145
this.memory_mut()
142146
.get_mut(buf.alloc_id)?
143147
.write_bytes(tcx, buf, &bytes)?;

0 commit comments

Comments
 (0)