0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

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.
This commit is contained in:
Ben Noordhuis 2012-06-12 16:04:56 +02:00
parent 408bfece51
commit 0385b17ce0

View File

@ -221,12 +221,7 @@ fs.readFileSync = function(path, encoding) {
} }
pos += bytesRead; pos += bytesRead;
done = (bytesRead === 0) || (size !== 0 && pos >= size);
if (size !== 0) {
done = pos >= size;
} else {
done = bytesRead >= 0;
}
} }
fs.closeSync(fd); fs.closeSync(fd);
@ -234,6 +229,8 @@ fs.readFileSync = function(path, encoding) {
if (size === 0) { if (size === 0) {
// data was collected into the buffers list. // data was collected into the buffers list.
buffer = Buffer.concat(buffers, pos); buffer = Buffer.concat(buffers, pos);
} else if (pos < size) {
buffer = buffer.slice(0, pos);
} }
if (encoding) buffer = buffer.toString(encoding); if (encoding) buffer = buffer.toString(encoding);