DrKLO/Telegram · error

Too many scans defined in file %s

Error message

Too many scans defined in file %s

What it means

The scan definition reader allocates a fixed-size array of MAX_SCANS (100) jpeg_scan_info structures on the stack. If the scan file defines more than 100 scan entries, scanno exceeds MAX_SCANS and the function reports the error, closes the file, and returns FALSE. This prevents a stack buffer overflow.

Source

Thrown at TMessagesProj/jni/mozjpeg/rdswitch.c:204

 */
{
  FILE *fp;
  int scanno, ncomps, termchar;
  long val;
  jpeg_scan_info *scanptr;
#define MAX_SCANS  100          /* quite arbitrary limit */
  jpeg_scan_info scans[MAX_SCANS];

  if ((fp = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "Can't open scan definition file %s\n", filename);
    return FALSE;
  }
  scanptr = scans;
  scanno = 0;

  while (read_scan_integer(fp, &val, &termchar)) {
    if (scanno >= MAX_SCANS) {
      fprintf(stderr, "Too many scans defined in file %s\n", filename);
      fclose(fp);
      return FALSE;
    }
    scanptr->component_index[0] = (int) val;
    ncomps = 1;
    while (termchar == ' ') {
      if (ncomps >= MAX_COMPS_IN_SCAN) {
        fprintf(stderr, "Too many components in one scan in file %s\n",
                filename);
        fclose(fp);
        return FALSE;
      }
      if (! read_scan_integer(fp, &val, &termchar))
        goto bogus;
      scanptr->component_index[ncomps] = (int) val;
      ncomps++;
    }
    scanptr->comps_in_scan = ncomps;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Reduce the number of scan entries to at most 100. Consolidate scans that can share component groupings.
  2. Use a simpler progressive scan script -- for most images, 10-30 scans is sufficient.
  3. Review the scan file and merge redundant or overly granular scan definitions.

Example fix

# before: scan file with 105 entries
# after: consolidate to fewer than 100 entries
# Use cjpeg built-in -progressive mode instead for simpler cases
cjpeg -progressive image.bmp > out.jpg
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate: count scan entries (terminated by ';') before calling the reader
static int count_scans(const char *filename) {
  FILE *fp = fopen(filename, "r");
  if (!fp) return -1;
  int c, count = 0;
  while ((c = fgetc(fp)) != EOF) {
    if (c == ';') count++;
  }
  fclose(fp);
  return count;
}
// if (count_scans(file) > 100) { too many }

Prevention

When it happens

Trigger: Providing a scan definition file with more than 100 scan entries. Each entry is terminated by a semicolon (';'); the scanno counter increments per entry.

Common situations: Overly granular progressive scan scripts; scripts generated by tools that create one scan per coefficient; misunderstanding the scan script syntax causing runaway parsing; very large all-coefficient progressive scripts.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/be64b819eb778bc5. Report an issue: GitHub.