From c67dc17a708d00627d3b67d9cb3f1dc9ac139701 Mon Sep 17 00:00:00 2001 From: Umuoy Date: Sun, 2 Apr 2023 01:01:50 +0800 Subject: [PATCH] buffer: check INSPECT_MAX_BYTES with validateNumber --- lib/buffer.js | 5 ++- .../test-buffer-set-inspect-max-bytes.js | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-buffer-set-inspect-max-bytes.js diff --git a/lib/buffer.js b/lib/buffer.js index fc177d01f06836..ec20e3432fcd61 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1401,7 +1401,10 @@ ObjectDefineProperties(module.exports, { configurable: true, enumerable: true, get() { return INSPECT_MAX_BYTES; }, - set(val) { INSPECT_MAX_BYTES = val; }, + set(val) { + validateNumber(val, 'INSPECT_MAX_BYTES', 0); + INSPECT_MAX_BYTES = val; + }, }, }); diff --git a/test/parallel/test-buffer-set-inspect-max-bytes.js b/test/parallel/test-buffer-set-inspect-max-bytes.js new file mode 100644 index 00000000000000..975c828111e5ec --- /dev/null +++ b/test/parallel/test-buffer-set-inspect-max-bytes.js @@ -0,0 +1,34 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const buffer = require('buffer'); + +const rangeErrorObjs = [NaN, -1]; +const typeErrorObj = 'and even this'; + +for (const obj of rangeErrorObjs) { + assert.throws( + () => buffer.INSPECT_MAX_BYTES = obj, + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + } + ); + + assert.throws( + () => buffer.INSPECT_MAX_BYTES = obj, + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + } + ); +} + +assert.throws( + () => buffer.INSPECT_MAX_BYTES = typeErrorObj, + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + } +);