Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fs: fix infinite loop in fs.readFileSync()

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 0385b17ce06b7c12ad3e4035477f6a0cf897580e
Showing with 3 additions and 6 deletions.
  1. +3 −6 lib/fs.js
@@ -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);

2 comments on commit 0385b17

@shigeki

This comment has been minimized.

Copy link

replied Jun 12, 2012

This and 408bfec are much robust. Good ones.

@bnoordhuis

This comment has been minimized.

Copy link
Member Author

replied Jun 12, 2012

Thanks. :-)

Please sign in to comment.
You can’t perform that action at this time.