FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fs: fix partial write corruption in writeFile and writeFileSync by olov · Pull Request #1063 · nodejs/node · GitHub

Repository navigation

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 11 additions & 5 deletions lib/fs.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,9 @@ function writeAll(fd, buffer, offset, length, position, callback) {
} else {
offset += written;
length -= written;
position += written;
if (position !== null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why the null check here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is to fix 2. position may be null (see fs.writeFile caller) and without the check position will become a number instead, pointing to the middle of an existing file => corrupting data.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, got it, makes sense.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think the check needs to be if (typeof position === 'number') {, else undefined or false will still trigger the bad behavior. It's also the condition that the documentation for fs.write() mentions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't see how position can become undefined or false in any of the two code paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

To elaborate, position is internal to writeFileSync and writeFile. In the latter case via an internal writeAll function that's only called from writeFile.

position += written;
}
writeAll(fd, buffer, offset, length, position, callback);
}
}
Expand Down Expand Up @@ -1146,13 +1148,17 @@ fs.writeFileSync = function(path, data, options) {
if (!(data instanceof Buffer)) {
data = new Buffer('' + data, options.encoding || 'utf8');
}
var written = 0;
var offset = 0;
var length = data.length;
var position = /a/.test(flag) ? null : 0;
try {
while (written < length) {
written += fs.writeSync(fd, data, written, length - written, position);
position += written;
while (length > 0) {
var written = fs.writeSync(fd, data, offset, length, position);
offset += written;
length -= written;
if (position !== null) {
position += written;
}
}
} finally {
fs.closeSync(fd);
Expand Down

Back | FazBrowse Home | New Git URL