{"record":{"id":"be64b819eb778bc5","repo":"DrKLO/Telegram","slug":"too-many-scans-defined-in-file-s","errorCode":null,"errorMessage":"Too many scans defined in file %s\n","messagePattern":"Too many scans defined in file (.+?)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/rdswitch.c","lineNumber":204,"sourceCode":" */\n{\n  FILE *fp;\n  int scanno, ncomps, termchar;\n  long val;\n  jpeg_scan_info *scanptr;\n#define MAX_SCANS  100          /* quite arbitrary limit */\n  jpeg_scan_info scans[MAX_SCANS];\n\n  if ((fp = fopen(filename, \"r\")) == NULL) {\n    fprintf(stderr, \"Can't open scan definition file %s\\n\", filename);\n    return FALSE;\n  }\n  scanptr = scans;\n  scanno = 0;\n\n  while (read_scan_integer(fp, &val, &termchar)) {\n    if (scanno >= MAX_SCANS) {\n      fprintf(stderr, \"Too many scans defined in file %s\\n\", filename);\n      fclose(fp);\n      return FALSE;\n    }\n    scanptr->component_index[0] = (int) val;\n    ncomps = 1;\n    while (termchar == ' ') {\n      if (ncomps >= MAX_COMPS_IN_SCAN) {\n        fprintf(stderr, \"Too many components in one scan in file %s\\n\",\n                filename);\n        fclose(fp);\n        return FALSE;\n      }\n      if (! read_scan_integer(fp, &val, &termchar))\n        goto bogus;\n      scanptr->component_index[ncomps] = (int) val;\n      ncomps++;\n    }\n    scanptr->comps_in_scan = ncomps;","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/rdswitch.c#L186-L222","documentation":"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.","triggerScenarios":"Providing a scan definition file with more than 100 scan entries. Each entry is terminated by a semicolon (';'); the scanno counter increments per entry.","commonSituations":"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.","solutions":["Reduce the number of scan entries to at most 100. Consolidate scans that can share component groupings.","Use a simpler progressive scan script -- for most images, 10-30 scans is sufficient.","Review the scan file and merge redundant or overly granular scan definitions."],"exampleFix":"# before: scan file with 105 entries\n# after: consolidate to fewer than 100 entries\n# Use cjpeg built-in -progressive mode instead for simpler cases\ncjpeg -progressive image.bmp > out.jpg","handlingStrategy":"validation","validationCode":"// Pre-validate: count scan entries (terminated by ';') before calling the reader\nstatic int count_scans(const char *filename) {\n  FILE *fp = fopen(filename, \"r\");\n  if (!fp) return -1;\n  int c, count = 0;\n  while ((c = fgetc(fp)) != EOF) {\n    if (c == ';') count++;\n  }\n  fclose(fp);\n  return count;\n}\n// if (count_scans(file) > 100) { too many }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Limit scan definition files to fewer than 100 entries.","Use cjpeg -progressive for standard progressive encoding instead of custom scan scripts.","Count semicolons in the scan file as a quick entry count check."],"tags":["validation","scan-definition","buffer-overflow-guard","mozjpeg"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}