string_decoder: fix crash when calling __proto__.write()

This makes the function throw an exception from JS instead of crashing.

Fixes: https://github.com/nodejs/node/issues/41949
Signed-off-by: Darshan Sen <raisinten@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/42062
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
This commit is contained in:
Darshan Sen authored and Bryan English committed 2022-03-20 17:27:04 -04:00
1 parent b89f4d5c17
commit 3492a0eb1e
2 files changed
+11

No files matched your search

+4
View File
@@ -43,6 +43,7 @@ const {
const internalUtil = require('internal/util');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
ERR_UNKNOWN_ENCODING
} = require('internal/errors').codes;
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
@@ -101,6 +102,9 @@ StringDecoder.prototype.write = function write(buf) {
throw new ERR_INVALID_ARG_TYPE('buf',
['Buffer', 'TypedArray', 'DataView'],
buf);
if (!this[kNativeDecoder]) {
throw new ERR_INVALID_THIS('StringDecoder');
}
return decode(this[kNativeDecoder], buf);
};