Skip to content

Commit 6079d72

Browse files
committed
Fix fd_readdir to properly truncate directory entry names.
Previously, `fd_readdir` was truncating directory entry names based on the calculation of `min(name_len, buf_len - bufused)`, but `bufused` was not being updated after writing in the `dirent` structure to the buffer. This allowed `bufused` to be incremented beyond `buf_len` and returned as the number of bytes written to the buffer, which is invalid. This fix adjusts `bufused` when the buffer is written to for the `dirent` so that name truncation happens as expected. Fixes #2618.
1 parent d1c1cb6 commit 6079d72

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

crates/wasi-common/src/snapshots/wasi_snapshot_preview1.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
304304
let dirent_len: types::Size = dirent_raw.len().try_into()?;
305305
let name_raw = name.as_bytes();
306306
let name_len = name_raw.len().try_into()?;
307-
let offset = dirent_len.checked_add(name_len).ok_or(Error::Overflow)?;
308307

309308
// Copy as many bytes of the dirent as we can, up to the end of the buffer.
310309
let dirent_copy_len = min(dirent_len, buf_len - bufused);
@@ -318,6 +317,9 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
318317
}
319318

320319
buf = buf.add(dirent_copy_len)?;
320+
bufused = bufused
321+
.checked_add(dirent_copy_len)
322+
.ok_or(Error::Overflow)?;
321323

322324
// Copy as many bytes of the name as we can, up to the end of the buffer.
323325
let name_copy_len = min(name_len, buf_len - bufused);
@@ -331,8 +333,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
331333
}
332334

333335
buf = buf.add(name_copy_len)?;
334-
335-
bufused += offset;
336+
bufused = bufused.checked_add(name_copy_len).ok_or(Error::Overflow)?;
336337
}
337338

338339
Ok(bufused)

0 commit comments

Comments
 (0)