diff --git a/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c b/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c
index daf135fe..5dd9a726 100644
--- a/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c
+++ b/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c
@@ -1880,7 +1880,7 @@ __wasi_errno_t wasmtime_ssp_fd_readdir(
}
*bufused = 0;
- while (nbyte > 0) {
+ while (*bufused < nbyte) {
// Read the next directory entry.
errno = 0;
struct dirent *de = readdir(dp);