Skip to content

Commit cf565b5

Browse files
Fishrock123brendanashworth
authored andcommitted
fs: fix .write() not coercing non-string values
Fixes: #1098 PR-URL: #1102 Reviewed-By: Brendan Ashworth <[email protected]>
1 parent 4bd3620 commit cf565b5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
629629
return binding.writeBuffer(fd, buffer, offset, length, position, req);
630630
}
631631

632-
if (typeof buffer === 'string')
632+
if (typeof buffer !== 'string')
633633
buffer += '';
634634
if (typeof position !== 'function') {
635635
if (typeof offset === 'function') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var path = require('path');
4+
var Buffer = require('buffer').Buffer;
5+
var fs = require('fs');
6+
var fn = path.join(common.tmpDir, 'write-string-coerce.txt');
7+
var data = true;
8+
var expected = data + '';
9+
var found;
10+
11+
fs.open(fn, 'w', 0644, function(err, fd) {
12+
if (err) throw err;
13+
console.log('open done');
14+
fs.write(fd, data, 0, 'utf8', function(err, written) {
15+
console.log('write done');
16+
if (err) throw err;
17+
assert.equal(Buffer.byteLength(expected), written);
18+
fs.closeSync(fd);
19+
found = fs.readFileSync(fn, 'utf8');
20+
console.log('expected: "%s"', expected);
21+
console.log('found: "%s"', found);
22+
fs.unlinkSync(fn);
23+
});
24+
});
25+
26+
27+
process.on('exit', function() {
28+
assert.equal(expected, found);
29+
});

0 commit comments

Comments
 (0)