fs: fix infinite loop in fs.readFile()
Fix an infinite loop in the case where the file got truncated by a concurrent writer while fs.readFile() was busy reading in the file.
This commit is contained in:
parent
e3a2dd1b13
commit
408bfece51
14
lib/fs.js
14
lib/fs.js
@ -148,18 +148,18 @@ fs.readFile = function(path, encoding_) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bytesRead === 0) {
|
||||||
|
return close();
|
||||||
|
}
|
||||||
|
|
||||||
pos += bytesRead;
|
pos += bytesRead;
|
||||||
if (size !== 0) {
|
if (size !== 0) {
|
||||||
if (pos === size) close();
|
if (pos === size) close();
|
||||||
else read();
|
else read();
|
||||||
} else {
|
} else {
|
||||||
// unknown size, just read until we don't get bytes.
|
// unknown size, just read until we don't get bytes.
|
||||||
if (bytesRead > 0) {
|
buffers.push(buffer.slice(0, bytesRead));
|
||||||
buffers.push(buffer.slice(0, bytesRead));
|
read();
|
||||||
read();
|
|
||||||
} else {
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,6 +168,8 @@ fs.readFile = function(path, encoding_) {
|
|||||||
if (size === 0) {
|
if (size === 0) {
|
||||||
// collected the data into the buffers list.
|
// collected the data 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…
x
Reference in New Issue
Block a user