checkByteSourceRange function

void checkByteSourceRange(
  1. ByteSource source,
  2. int offset,
  3. int length
)

Validates a read(offset, length) request against source's length.

Shared helper for ByteSource implementations: throws ArgumentError for negative values (programmer error) and UnexpectedEofException when the range extends past the end of the source (attacker-controlled header fields land here).

Implementation

void checkByteSourceRange(ByteSource source, int offset, int length) {
  if (offset < 0) {
    throw ArgumentError.value(offset, 'offset', 'must be non-negative');
  }
  if (length < 0) {
    throw ArgumentError.value(length, 'length', 'must be non-negative');
  }
  if (offset + length > source.length) {
    throw UnexpectedEofException(
      'read of $length byte(s) at offset $offset extends past the end of '
      'the ${source.length}-byte source',
      offset: offset,
    );
  }
}