mozilla/pdf.js · error · Error

Invalid number of params in printf

Error message

Invalid number of params in printf

What it means

Thrown by `Util.printf()` (src/scripting_api/util.js:66) when called with zero arguments. `printf` mirrors C's `printf`: the first argument is the format string and subsequent arguments fill its conversion specifiers. No arguments means no format string at all.

Source

Thrown at src/scripting_api/util.js:66

    "Thursday",
    "Friday",
    "Saturday",
  ];

  MILLISECONDS_IN_DAY = 86400000;

  MILLISECONDS_IN_WEEK = 604800000;

  constructor(data) {
    super(data);

    // used with crackURL
    this._externalCall = data.externalCall;
  }

  printf(...args) {
    if (args.length === 0) {
      throw new Error("Invalid number of params in printf");
    }

    if (typeof args[0] !== "string") {
      throw new TypeError("First argument of printf must be a string");
    }

    // eslint-disable-next-line regexp/no-misleading-capturing-group
    const pattern = /%(,[0-4])?([+ 0#]+)?(\d+)?(\.\d+)?(.)/g;
    const PLUS = 1;
    const SPACE = 2;
    const ZERO = 4;
    const HASH = 8;
    let i = 0;
    return args[0].replaceAll(
      pattern,
      function (_, nDecSep, cFlags, nWidth, nPrecision, cConvChar) {
        // cConvChar must be one of d, f, s, x
        if (

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Always pass at least the format string: `util.printf('%s', value)`.
  2. Default the format argument: `util.printf(fmt || '%s', value)`.
  3. Guard the call site: skip `printf` entirely when there is nothing to format.

Example fix

// before
util.printf(...args); // args may be []
// after
if (args.length === 0) return '';
return util.printf(...args);
Defensive patterns

Strategy: validation

Validate before calling

function printfSafe(util, ...args) {
  if (args.length === 0) return '';
  return util.printf(...args);
}

Type guard

function hasFormatArg(args) {
  return Array.isArray(args) && args.length > 0;
}

Prevention

When it happens

Trigger: Calling `util.printf()` with no arguments, or `util.printf(...emptyArray)` where the array was unexpectedly empty. Often a missing return value from a helper that was supposed to supply the format.

Common situations: Dynamic format strings built from optional configuration where the configuration object was empty; refactoring that introduced an extra call layer swallowing the format argument.

Related errors


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