diff options
author | cjihrig <cjihrig@gmail.com> | 2021-04-09 21:39:43 -0400 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2021-04-11 23:57:29 -0400 |
commit | 8e76397fab69f79dfd77147360e20e004b65beed (patch) | |
tree | 0ab6b6b44b86480167d198241aadc94a3de0c259 /lib/fs.js | |
parent | 28bca33f284f5f76a7f934fa6b60c586e5cfc948 (diff) | |
download | node-new-8e76397fab69f79dfd77147360e20e004b65beed.tar.gz |
fs: validate encoding to binding.writeString()
The binding layer performs some validation of the encoding and
data passed to WriteString(). This commit adds similar validation
to the JS layer for better error handling.
PR-URL: https://github.com/nodejs/node/pull/38183
Fixes: https://github.com/nodejs/node/issues/38168
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Diffstat (limited to 'lib/fs.js')
-rw-r--r-- | lib/fs.js | 7 |
1 files changed, 6 insertions, 1 deletions
@@ -133,6 +133,7 @@ const { validateBoolean, validateBuffer, validateCallback, + validateEncoding, validateFunction, validateInteger, } = require('internal/validators'); @@ -702,11 +703,14 @@ function write(fd, buffer, offset, length, position, callback) { } length = 'utf8'; } + + const str = String(buffer); + validateEncoding(str, length); callback = maybeCallback(position); const req = new FSReqCallback(); req.oncomplete = wrapper; - return binding.writeString(fd, String(buffer), offset, length, req); + return binding.writeString(fd, str, offset, length, req); } ObjectDefineProperty(write, internalUtil.customPromisifyArgs, @@ -735,6 +739,7 @@ function writeSync(fd, buffer, offset, length, position) { undefined, ctx); } else { validateStringAfterArrayBufferView(buffer, 'buffer'); + validateEncoding(buffer, length); if (offset === undefined) offset = null; |