From 6744e59e461cc200a1c135c99450b2800c21b5e9 Mon Sep 17 00:00:00 2001 From: Marco Rogers Date: Tue, 17 Aug 2010 00:10:49 -0400 Subject: [PATCH] detect 0 length fs writes with tests --- lib/fs.js | 2 ++ test/simple/test-fs-write-sync.js | 4 ++++ test/simple/test-fs-write.js | 3 +++ 3 files changed, 9 insertions(+) diff --git a/lib/fs.js b/lib/fs.js index 6fc0e5f798..a6449858f8 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -208,6 +208,7 @@ fs.write = function (fd, buffer, offset, length, position, callback) { offset = 0; length = buffer.length; } + if(!length) return; binding.write(fd, buffer, offset, length, position, callback || noop); }; @@ -221,6 +222,7 @@ fs.writeSync = function (fd, buffer, offset, length, position) { offset = 0; length = buffer.length; } + if(!length) return 0; return binding.write(fd, buffer, offset, length, position); }; diff --git a/test/simple/test-fs-write-sync.js b/test/simple/test-fs-write-sync.js index 380aa72f77..5251c49ab4 100644 --- a/test/simple/test-fs-write-sync.js +++ b/test/simple/test-fs-write-sync.js @@ -8,6 +8,10 @@ fn = path.join(common.fixturesDir, 'write.txt'); foo = 'foo' var fd = fs.openSync(fn, 'w'); + +written = fs.writeSync(fd, ''); +assert.strictEqual(0, written); + fs.writeSync(fd, foo); bar = 'bár' diff --git a/test/simple/test-fs-write.js b/test/simple/test-fs-write.js index 8ac3f422b6..c371c06792 100644 --- a/test/simple/test-fs-write.js +++ b/test/simple/test-fs-write.js @@ -10,6 +10,9 @@ var found; fs.open(fn, 'w', 0644, function (err, fd) { if (err) throw err; console.log('open done'); + fs.write(fd, '', 0, 'utf8', function(err, written) { + assert.fail('zero length write should not go through to callback'); + }); fs.write(fd, expected, 0, "utf8", function (err, written) { console.log('write done'); if (err) throw err;