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.readFileSync()
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.readFileSync() was busy reading in the file.
  • Loading branch information
bnoordhuis committed Jun 12, 2012
1 parent 408bfec commit 0385b17
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/fs.js
Expand Up @@ -221,19 +221,16 @@ fs.readFileSync = function(path, encoding) {
}

pos += bytesRead;

if (size !== 0) {
done = pos >= size;
} else {
done = bytesRead >= 0;
}
done = (bytesRead === 0) || (size !== 0 && pos >= size);
}

fs.closeSync(fd);

if (size === 0) {
// data was collected into the buffers list.
buffer = Buffer.concat(buffers, pos);
} else if (pos < size) {
buffer = buffer.slice(0, pos);
}

if (encoding) buffer = buffer.toString(encoding);
Expand Down

2 comments on commit 0385b17

@shigeki
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and 408bfec are much robust. Good ones.

@bnoordhuis
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. :-)

Please sign in to comment.