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.
pull/24503/head
Ben Noordhuis 2012-06-12 16:04:56 +02:00
parent 408bfece51
commit 0385b17ce0
1 changed files with 3 additions and 6 deletions

View File

@ -221,12 +221,7 @@ 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);
@ -234,6 +229,8 @@ fs.readFileSync = function(path, encoding) {
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);