Browse Source

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.
v0.9.1-release
Ben Noordhuis 13 years ago
parent
commit
0385b17ce0
  1. 9
      lib/fs.js

9
lib/fs.js

@ -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);

Loading…
Cancel
Save