buffer: convert values to uint, not int
authorTrevor Norris <trev.norris@gmail.com>
Thu, 14 Nov 2013 23:40:56 +0000 (15:40 -0800)
committerTrevor Norris <trev.norris@gmail.com>
Fri, 15 Nov 2013 19:48:09 +0000 (11:48 -0800)
In many cases values expected to be unsigned were converted to a signed
integer.

Also include some small code cleanup.

lib/buffer.js

index bfaee2c..b433630 100644 (file)
@@ -33,9 +33,7 @@ exports.INSPECT_MAX_BYTES = 50;
 
 
 Buffer.poolSize = 8 * 1024;
-var poolSize = Buffer.poolSize;
-var poolOffset = 0;
-var allocPool = alloc({}, poolSize);
+var poolSize, poolOffset, allocPool;
 
 
 function createPool() {
@@ -43,6 +41,7 @@ function createPool() {
   allocPool = alloc({}, poolSize);
   poolOffset = 0;
 }
+createPool();
 
 
 function Buffer(subject, encoding) {
@@ -50,7 +49,7 @@ function Buffer(subject, encoding) {
     return new Buffer(subject, encoding);
 
   if (util.isNumber(subject))
-    this.length = subject > 0 ? Math.floor(subject) : 0;
+    this.length = subject > 0 ? subject >>> 0 : 0;
   else if (util.isString(subject))
     this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8');
   else if (util.isObject(subject))
@@ -63,7 +62,7 @@ function Buffer(subject, encoding) {
                          'size: 0x' + kMaxLength.toString(16) + ' bytes');
   }
 
-  if (this.length < Buffer.poolSize / 2 && this.length > 0) {
+  if (this.length <= (Buffer.poolSize >>> 1) && this.length > 0) {
     if (this.length > poolSize - poolOffset)
       createPool();
     this.parent = sliceOnto(allocPool,
@@ -154,11 +153,9 @@ Buffer.concat = function(list, length) {
     for (var i = 0; i < list.length; i++)
       length += list[i].length;
   } else {
-    length = ~~length;
+    length = length >>> 0;
   }
 
-  if (length < 0) length = 0;
-
   if (list.length === 0)
     return new Buffer(0);
   else if (list.length === 1)
@@ -291,19 +288,21 @@ var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
 Buffer.prototype.write = function(string, offset, length, encoding) {
   // Buffer#write(string);
   if (util.isUndefined(offset)) {
-    offset = 0;
     encoding = 'utf8';
+    length = this.length;
+    offset = 0;
 
   // Buffer#write(string, encoding)
   } else if (util.isUndefined(length) && util.isString(offset)) {
     encoding = offset;
+    length = this.length;
     offset = 0;
 
   // Buffer#write(string, offset[, length][, encoding])
   } else if (isFinite(offset)) {
-    offset = ~~offset;
+    offset = offset >>> 0;
     if (isFinite(length)) {
-      length = ~~length;
+      length = length >>> 0;
       if (util.isUndefined(encoding))
         encoding = 'utf8';
     } else {
@@ -325,7 +324,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
 
     var swap = encoding;
     encoding = offset;
-    offset = ~~length;
+    offset = length >>> 0;
     length = swap;
   }
 
@@ -336,7 +335,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
   encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8';
 
   if (string.length > 0 && (length < 0 || offset < 0))
-    throw new RangeError('attempt to write beyond buffer bounds');
+    throw new RangeError('attempt to write outside buffer bounds');
 
   var ret;
   switch (encoding) {
@@ -422,13 +421,13 @@ Buffer.prototype.slice = function(start, end) {
 
 
 function checkOffset(offset, ext, length) {
-  if (offset < 0 || offset + ext > length)
+  if (offset + ext > length)
     throw new RangeError('index out of range');
 }
 
 
 Buffer.prototype.readUInt8 = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 1, this.length);
   return this[offset];
@@ -449,7 +448,7 @@ function readUInt16(buffer, offset, isBigEndian) {
 
 
 Buffer.prototype.readUInt16LE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 2, this.length);
   return readUInt16(this, offset, false, noAssert);
@@ -457,7 +456,7 @@ Buffer.prototype.readUInt16LE = function(offset, noAssert) {
 
 
 Buffer.prototype.readUInt16BE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 2, this.length);
   return readUInt16(this, offset, true, noAssert);
@@ -482,7 +481,7 @@ function readUInt32(buffer, offset, isBigEndian) {
 
 
 Buffer.prototype.readUInt32LE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 4, this.length);
   return readUInt32(this, offset, false);
@@ -490,7 +489,7 @@ Buffer.prototype.readUInt32LE = function(offset, noAssert) {
 
 
 Buffer.prototype.readUInt32BE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 4, this.length);
   return readUInt32(this, offset, true);
@@ -544,25 +543,22 @@ Buffer.prototype.readUInt32BE = function(offset, noAssert) {
  */
 
 Buffer.prototype.readInt8 = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 1, this.length);
-  if (!(this[offset] & 0x80))
-    return (this[offset]);
-  return ((0xff - this[offset] + 1) * -1);
+  var val = this[offset];
+  return !(val & 0x80) ? val : (0xff - val + 1) * -1;
 };
 
 
 function readInt16(buffer, offset, isBigEndian) {
   var val = readUInt16(buffer, offset, isBigEndian);
-  if (!(val & 0x8000))
-    return val;
-  return (0xffff - val + 1) * -1;
+  return !(val & 0x8000) ? val : (0xffff - val + 1) * -1;
 }
 
 
 Buffer.prototype.readInt16LE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 2, this.length);
   return readInt16(this, offset, false);
@@ -570,7 +566,7 @@ Buffer.prototype.readInt16LE = function(offset, noAssert) {
 
 
 Buffer.prototype.readInt16BE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 2, this.length);
   return readInt16(this, offset, true);
@@ -579,14 +575,12 @@ Buffer.prototype.readInt16BE = function(offset, noAssert) {
 
 function readInt32(buffer, offset, isBigEndian) {
   var val = readUInt32(buffer, offset, isBigEndian);
-  if (!(val & 0x80000000))
-    return (val);
-  return (0xffffffff - val + 1) * -1;
+  return !(val & 0x80000000) ? val : (0xffffffff - val + 1) * -1;
 }
 
 
 Buffer.prototype.readInt32LE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 4, this.length);
   return readInt32(this, offset, false);
@@ -594,7 +588,7 @@ Buffer.prototype.readInt32LE = function(offset, noAssert) {
 
 
 Buffer.prototype.readInt32BE = function(offset, noAssert) {
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkOffset(offset, 4, this.length);
   return readInt32(this, offset, true);
@@ -602,16 +596,18 @@ Buffer.prototype.readInt32BE = function(offset, noAssert) {
 
 
 function checkInt(buffer, value, offset, ext, max, min) {
+  if (!(buffer instanceof Buffer))
+    throw new TypeError('buffer must be a Buffer instance');
   if (value > max || value < min)
     throw new TypeError('value is out of bounds');
-  if (offset < 0 || offset + ext > buffer.length || buffer.length + offset < 0)
+  if (offset + ext > buffer.length)
     throw new RangeError('index out of range');
 }
 
 
 Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 1, 0xff, 0);
   this[offset] = value;
@@ -633,7 +629,7 @@ function writeUInt16(buffer, value, offset, isBigEndian) {
 
 Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 2, 0xffff, 0);
   return writeUInt16(this, value, offset, false);
@@ -642,7 +638,7 @@ Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
 
 Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 2, 0xffff, 0);
   return writeUInt16(this, value, offset, true);
@@ -667,7 +663,7 @@ function writeUInt32(buffer, value, offset, isBigEndian) {
 
 Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 4, 0xffffffff, 0);
   return writeUInt32(this, value, offset, false);
@@ -676,7 +672,7 @@ Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
 
 Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 4, 0xffffffff, 0);
   return writeUInt32(this, value, offset, true);
@@ -722,7 +718,7 @@ Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
 
 Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 1, 0x7f, -0x80);
   if (value < 0) value = 0xff + value + 1;
@@ -733,7 +729,7 @@ Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
 
 Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 2, 0x7fff, -0x8000);
   if (value < 0) value = 0xffff + value + 1;
@@ -743,7 +739,7 @@ Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
 
 Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 2, 0x7fff, -0x8000);
   if (value < 0) value = 0xffff + value + 1;
@@ -753,7 +749,7 @@ Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
 
 Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
   if (value < 0) value = 0xffffffff + value + 1;
@@ -763,7 +759,7 @@ Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
 
 Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
   value = +value;
-  offset = ~~offset;
+  offset = offset >>> 0;
   if (!noAssert)
     checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
   if (value < 0) value = 0xffffffff + value + 1;