mozilla/pdf.js · error · Error

mapBfRange - ignoring data above MAX_MAP_RANGE.

Error message

mapBfRange - ignoring data above MAX_MAP_RANGE.

What it means

Thrown by CMap.mapBfRange when a beginbfrange entry spans more than MAX_MAP_RANGE (2^24 - 1) code points. Same memory guard as mapCidRange but for the string-destination bf range form used in ToUnicode CMaps. Indicates a malformed or hostile ToUnicode stream.

Source

Thrown at src/core/cmap.js:236

  }

  addCodespaceRange(n, low, high) {
    this.codespaceRanges[n - 1].push(low, high);
    this.numCodespaceRanges++;
  }

  mapCidRange(low, high, dstLow) {
    if (high - low > MAX_MAP_RANGE) {
      throw new Error("mapCidRange - ignoring data above MAX_MAP_RANGE.");
    }
    while (low <= high) {
      this._map[low++] = dstLow++;
    }
  }

  mapBfRange(low, high, dstLow) {
    if (high - low > MAX_MAP_RANGE) {
      throw new Error("mapBfRange - ignoring data above MAX_MAP_RANGE.");
    }
    const lastByte = dstLow.length - 1;
    while (low <= high) {
      this._map[low++] = dstLow;
      // Only the last byte has to be incremented (in the normal case).
      const nextCharCode = dstLow.charCodeAt(lastByte) + 1;
      if (nextCharCode > 0xff) {
        dstLow =
          dstLow.substring(0, lastByte - 1) +
          String.fromCharCode(dstLow.charCodeAt(lastByte - 1) + 1) +
          "\x00";
        continue;
      }
      dstLow =
        dstLow.substring(0, lastByte) + String.fromCharCode(nextCharCode);
    }
  }

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Repair or regenerate the PDF; an authoritative reader should also reject it.
  2. Update pdf.js for stricter/safer CMap handling.
  3. Split the offending bfrange into multiple ranges each <= 0xFFFFFF wide if you author CMaps.
  4. Catch the error and continue without ToUnicode (text selection accuracy degrades but rendering works).

Example fix

// before
const cmap = await CMapFactory.create({ encoding, fetchBuiltInCMap, useCMap });

// after
let cmap;
try {
  cmap = await CMapFactory.create({ encoding, fetchBuiltInCMap, useCMap });
} catch (e) {
  if (/MAX_MAP_RANGE/.test(e.message)) {
    console.warn('Oversized CMap bf range, skipping', e);
    cmap = null;
  } else throw e;
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  cmap = await CMapFactory.create({ encoding, fetchBuiltInCMap, useCMap });
} catch (e) {
  if (/MAX_MAP_RANGE/.test(e.message)) {
    console.warn('Oversized bf range in CMap, skipping', e);
    cmap = null;
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Parsing a CMap whose beginbfrange ... endbfrange block contains <low> <high> <dst-string> where high - low > 0xFFFFFF. Hit while building a font's ToUnicode mapping for text search/selection/copy.

Common situations: Corrupt ToUnicode CMap emitted by a broken PDF writer; malicious PDF designed to balloon memory; truncated CMap reassembled incorrectly.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/c16a702efc52369d. Report an issue: GitHub.