Skip to content

Commit 9a64cec

Browse files
mscdexMylesBorins
authored andcommitted
buffer: fix writeUInt16BE range check
Fixes: #24205 PR-URL: #24208 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent d7a3a3b commit 9a64cec

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/internal/buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ function writeU_Int16BE(buf, value, offset, min, max) {
658658
}
659659

660660
function writeUInt16BE(value, offset = 0) {
661-
return writeU_Int16BE(this, value, offset, 0, 0xffffffff);
661+
return writeU_Int16BE(this, value, offset, 0, 0xffff);
662662
}
663663

664664
function writeIntLE(value, offset = 0, byteLength) {

test/parallel/test-buffer-writeuint.js

+12
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ const assert = require('assert');
8484

8585
data.writeUInt16BE(value, 0);
8686
assert.ok(data.equals(new Uint8Array([0xff, 0x80, 0x43, 0x23])));
87+
88+
value = 0xfffff;
89+
['writeUInt16BE', 'writeUInt16LE'].forEach((fn) => {
90+
assert.throws(
91+
() => data[fn](value, 0),
92+
{
93+
code: 'ERR_OUT_OF_RANGE',
94+
message: 'The value of "value" is out of range. ' +
95+
`It must be >= 0 and <= 65535. Received ${value}`
96+
}
97+
);
98+
});
8799
}
88100

89101
// Test 32 bit

0 commit comments

Comments
 (0)