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

@olov

1. writeFileSync bumps position incorrectly, causing it to
drift in iteration three and onwards.

2. Append mode files will get corrupted in the middle if
writeFile or writeFileSync iterates multiple times, unless
running on Linux. position starts out as null so first write is
OK, but then position will refer to a location inside an
existing file, corrupting that data. Linux ignores position for
append mode files so it doesn't happen there.

This commit fixes these two related issues by bumping position
correctly and by always using null as the position argument
to write/writeSync for append mode files.

@piscisaureus mentioned this pull request

Mar 5, 2015

@mscdex mscdex added the fs

Issues and PRs related to the fs subsystem / file system.

label

Mar 22, 2015

piscisaureus pushed a commit that referenced this pull request

Mar 25, 2015
1. writeFileSync bumps position incorrectly, causing it to drift in
iteration three and onwards.

2. Append mode files will get corrupted in the middle if writeFile or
writeFileSync iterates multiple times, unless running on Linux. position
starts out as null so first write is OK, but then position will refer to
a location inside an existing file, corrupting that data. Linux ignores
position for append mode files so it doesn't happen there.

This commit fixes these two related issues by bumping position correctly
and by always using null as the position argument to write/writeSync for
append mode files.

PR-URL: #1063
Reviewed-By: Bert Belder <bertbelder@gmail.com>

@rvagg rvagg mentioned this pull request

Mar 28, 2015

rvagg added a commit that referenced this pull request

Mar 31, 2015
Notable changes:

 * fs: corruption can be caused by fs.writeFileSync() and append-mode
   fs.writeFile() and fs.writeFileSync() under certain circumstances,
   reported in #1058, fixed in #1063 (Olov Lassus).
 * iojs: an "internal modules" API has been introduced to allow core
   code to share JavaScript modules internally only without having to
   expose them as a public API, this feature is for core-only #848
   (Vladimir Kurchatkin).
 * timers: two minor problems with timers have been fixed:
   - Timer#close() is now properly idempotent #1288 (Petka Antonov).
   - setTimeout() will only run the callback once now after an
     unref() during the callback #1231 (Roman Reiss).
 * Windows: a "delay-load hook" has been added for compiled add-ons
   on Windows that should alleviate some of the problems that Windows
   users may be experiencing with add-ons in io.js #1251
   (Bert Belder).
 * V8: minor bug-fix upgrade for V8 to 4.1.0.27.
 * npm: upgrade npm to 2.7.4. See npm CHANGELOG.md for details.