buffer: check INSPECT_MAX_BYTES with validateNumber

PR-URL: https://github.com/nodejs/node/pull/46599
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Umuoy 2023-04-03 16:24:24 +08:00 committed by GitHub
parent 9a31ac19f4
commit 1948d37595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -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;
},
},
});

View File

@ -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',
}
);