Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
fs: fix infinite loop in fs.readFile()
Browse files Browse the repository at this point in the history
Fix an infinite loop in the case where the file got truncated by a concurrent
writer while fs.readFile() was busy reading in the file.
  • Loading branch information
bnoordhuis committed Jun 12, 2012
1 parent e3a2dd1 commit 8f1aaee
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/fs.js
Expand Up @@ -148,18 +148,18 @@ fs.readFile = function(path, encoding_) {
});
}

if (bytesRead === 0) {
return close();
}

pos += bytesRead;
if (size !== 0) {
if (pos === size) close();
else read();
} else {
// unknown size, just read until we don't get bytes.
if (bytesRead > 0) {
buffers.push(buffer.slice(0, bytesRead));
read();
} else {
close();
}
buffers.push(buffer.slice(0, bytesRead));
read();
}
}

Expand Down

0 comments on commit 8f1aaee

Please sign in to comment.