Repository: nodejs/node
File: lib/buffer.js (JavaScript)

Code before:
  return _compare(buf1, buf2);
};

Buffer.isEncoding = function isEncoding(encoding) {
  return typeof encoding === 'string' && encoding.length !== 0 &&
         normalizeEncoding(encoding) !== undefined;
};
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Buffer.concat = function concat(list, length) {
  validateArray(list, 'list');

  if (list.length === 0)
    return new FastBuffer();

  if (length === undefined) {
    length = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      if (!isUint8Array(buf)) {
        // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
        // Instead, find the proper error code for this.
        throw new ERR_INVALID_ARG_TYPE(
          `list[${i}]`, ['Buffer', 'Uint8Array'], buf);
      }
      length += buf.length;
    }

    const buffer = allocate(length);
    let pos = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      TypedArrayPrototypeSet(buffer, buf, pos);
      pos += buf.length;
    }

    if (pos < length) {
      TypedArrayPrototypeFill(buffer, 0, pos, length);
    }
    return buffer;
  }

  validateOffset(length, 'length');
  for (let i = 0; i < list.length; i++) {
    if (!isUint8Array(list[i])) {
      // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
      // Instead, find the proper error code for this.
      throw new ERR_INVALID_ARG_TYPE(
        `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);
    }
  }

Review comment (bug):
While it now shouldn't be possible to overflow, would it still be worthwhile to use `TypedArrayPrototypeGetLength` in the places where `buf.length` is accessed?

Code after:

  return _compare(buf1, buf2);
};

Buffer.isEncoding = function isEncoding(encoding) {
  return typeof encoding === 'string' && encoding.length !== 0 &&
         normalizeEncoding(encoding) !== undefined;
};
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

Buffer.concat = function concat(list, length) {
  validateArray(list, 'list');

  if (list.length === 0)
    return new FastBuffer();

  if (length === undefined) {
    length = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      if (!isUint8Array(buf)) {
        // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
        // Instead, find the proper error code for this.
        throw new ERR_INVALID_ARG_TYPE(
          `list[${i}]`, ['Buffer', 'Uint8Array'], buf);
      }
      length += TypedArrayPrototypeGetByteLength(buf);
    }

    const buffer = allocate(length);
    let pos = 0;
    for (let i = 0; i < list.length; i++) {
      const buf = list[i];
      const bufLength = TypedArrayPrototypeGetByteLength(buf);
      TypedArrayPrototypeSet(buffer, buf, pos);
      pos += bufLength;
    }

    if (pos < length) {
      TypedArrayPrototypeFill(buffer, 0, pos, length);
    }
    return buffer;
  }

  validateOffset(length, 'length');
  for (let i = 0; i < list.length; i++) {
    if (!isUint8Array(list[i])) {
      // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
      // Instead, find the proper error code for this.
      throw new ERR_INVALID_ARG_TYPE(
        `list[${i}]`, ['Buffer', 'Uint8Array'], list[i]);