Skip to content

Commit

Permalink
buffer: check INSPECT_MAX_BYTES with validateNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
umuoy1 committed Apr 1, 2023
1 parent 85705a4 commit c67dc17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
});

Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-buffer-set-inspect-max-bytes.js
Original file line number Diff line number Diff line change
@@ -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',
}
);

0 comments on commit c67dc17

Please sign in to comment.