Skip to content

Commit 1d0f2cb

Browse files
trevnorrisjasnell
authored andcommitted
buffer: fix value check for writeUInt{B,L}E
Fixes: #3497 PR-URL: #3500 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 0429131 commit 1d0f2cb

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎lib/buffer.js‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,8 +836,10 @@ Buffer.prototype.writeUIntLE = function(value, offset, byteLength, noAssert) {
836836
value=+value;
837837
offset=offset>>>0;
838838
byteLength=byteLength>>>0;
839-
if(!noAssert)
840-
checkInt(this,value,offset,byteLength,Math.pow(2,8*byteLength),0);
839+
if(!noAssert){
840+
constmaxBytes=Math.pow(2,8*byteLength)-1;
841+
checkInt(this,value,offset,byteLength,maxBytes,0);
842+
}
841843

842844
varmul=1;
843845
vari=0;
@@ -853,8 +855,10 @@ Buffer.prototype.writeUIntBE = function(value, offset, byteLength, noAssert) {
853855
value=+value;
854856
offset=offset>>>0;
855857
byteLength=byteLength>>>0;
856-
if(!noAssert)
857-
checkInt(this,value,offset,byteLength,Math.pow(2,8*byteLength),0);
858+
if(!noAssert){
859+
constmaxBytes=Math.pow(2,8*byteLength)-1;
860+
checkInt(this,value,offset,byteLength,maxBytes,0);
861+
}
858862

859863
vari=byteLength-1;
860864
varmul=1;

‎test/parallel/test-writeuint.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ function test32(clazz) {
122122
}
123123

124124

125+
functiontestUint(clazz){
126+
constdata=newclazz(8);
127+
varval=1;
128+
129+
// Test 0 to 5 bytes.
130+
for(vari=0;i<=5;i++){
131+
consterrmsg=`byteLength: ${i}`;
132+
ASSERT.throws(function(){
133+
data.writeUIntBE(val,0,i);
134+
},/valueisoutofbounds/,errmsg);
135+
ASSERT.throws(function(){
136+
data.writeUIntLE(val,0,i);
137+
},/valueisoutofbounds/,errmsg);
138+
val*=0x100;
139+
}
140+
}
141+
142+
125143
test8(Buffer);
126144
test16(Buffer);
127145
test32(Buffer);
146+
testUint(Buffer);

0 commit comments

Comments
 (0)