Skip to content

[v14.x backport] fs: fix write methods param validation and docs

Backport of https://github.com/nodejs/node/pull/42588 and https://github.com/nodejs/node/pull/41677. This fixes a bug that makes the process crash if a non-buffer object was passed to FileHandle.prototype.write:

$ node -e 'console.log(process.version);fs.promises.open("/dev/null").then(fh => fh.write({ toString() { return "str\n" } }))'
v14.19.1
/usr/local/opt/node@14/bin/node[13975]: ../src/string_bytes.cc:319:static size_t node::StringBytes::Write(v8::Isolate *, char *, size_t, Local<v8::Value>, enum encoding, int *): Assertion `val->IsString() == true' failed.
 1: 0x1000a6e4a node::Abort() [/usr/local/Cellar/node@14/14.19.1/bin/node]
 2: 0x1000a6cd3 node::AppendExceptionLine(node::Environment*, v8::Local<v8::Value>, v8::Local<v8::Message>, node::ErrorHandlingMode) [/usr/local/Cellar/node@14/14.19.1/bin/node]
 3: 0x1001485c0 node::StringBytes::Write(v8::Isolate*, char*, unsigned long, v8::Local<v8::Value>, node::encoding, int*) [/usr/local/Cellar/node@14/14.19.1/bin/node]
 4: 0x1000b69f8 node::fs::WriteString(v8::FunctionCallbackInfo<v8::Value> const&) [/usr/local/Cellar/node@14/14.19.1/bin/node]
 5: 0x100205198 v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) [/usr/local/Cellar/node@14/14.19.1/bin/node]
 6: 0x1002048a1 v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/usr/local/Cellar/node@14/14.19.1/bin/node]
 7: 0x100204012 v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/usr/local/Cellar/node@14/14.19.1/bin/node]
 8: 0x1007ca639 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit [/usr/local/Cellar/node@14/14.19.1/bin/node]
 9: 0x1007610a2 Builtins_InterpreterEntryTrampoline [/usr/local/Cellar/node@14/14.19.1/bin/node]
10: 0x1007610a2 Builtins_InterpreterEntryTrampoline [/usr/local/Cellar/node@14/14.19.1/bin/node]
11: 0x10075afd9 Builtins_ArgumentsAdaptorTrampoline [/usr/local/Cellar/node@14/14.19.1/bin/node]
[1]    13975 abort      /usr/local/opt/node@14/bin/node -e
$ out/Release/node -e 'console.log(process.version);fs.promises.open("/dev/null").then(fh => fh.write({ toString() { return "str\n" } }))'
v14.19.2-pre
(node:16354) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "buffer" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object
    at write (internal/fs/promises.js:464:3)
    at fsCall (internal/fs/promises.js:243:18)
    at FileHandle.write (internal/fs/promises.js:160:12)
    at [eval]:1:74
(Use `node --trace-warnings ...` to show where the warning was created)
(node:16354) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:16354) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:16354) Warning: Closing file descriptor 23 on garbage collection
(node:16354) [DEP0137] DeprecationWarning: Closing a FileHandle object on garbage collection is deprecated. Please close FileHandle objects explicitly using FileHandle.prototype.close(). In the future, an error will be thrown if a file descriptor is closed during garbage collection.

Merge request reports

Loading