{"record":{"id":"a2589ed8f2e386f6","repo":"DrKLO/Telegram","slug":"s-can-t-allocate-memory-for-icc-profile-a2589e","errorCode":null,"errorMessage":"%s: can't allocate memory for ICC profile\n","messagePattern":"(.+?): can't allocate memory for ICC profile\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/jpegtran.c","lineNumber":517,"sourceCode":"  } else {\n    /* default input file is stdin */\n    fp = read_stdin();\n  }\n\n  if (icc_filename != NULL) {\n    if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) {\n      fprintf(stderr, \"%s: can't open %s\\n\", progname, icc_filename);\n      exit(EXIT_FAILURE);\n    }\n    if (fseek(icc_file, 0, SEEK_END) < 0 ||\n        (icc_len = ftell(icc_file)) < 1 ||\n        fseek(icc_file, 0, SEEK_SET) < 0) {\n      fprintf(stderr, \"%s: can't determine size of %s\\n\", progname,\n              icc_filename);\n      exit(EXIT_FAILURE);\n    }\n    if ((icc_profile = (JOCTET *)malloc(icc_len)) == NULL) {\n      fprintf(stderr, \"%s: can't allocate memory for ICC profile\\n\", progname);\n      fclose(icc_file);\n      exit(EXIT_FAILURE);\n    }\n    if (fread(icc_profile, icc_len, 1, icc_file) < 1) {\n      fprintf(stderr, \"%s: can't read ICC profile from %s\\n\", progname,\n              icc_filename);\n      free(icc_profile);\n      fclose(icc_file);\n      exit(EXIT_FAILURE);\n    }\n    fclose(icc_file);\n    if (copyoption == JCOPYOPT_ALL)\n      copyoption = JCOPYOPT_ALL_EXCEPT_ICC;\n  }\n\n#ifdef PROGRESS_REPORT\n  start_progress_monitor((j_common_ptr)&dstinfo, &progress);\n#endif","sourceCodeStart":499,"sourceCodeEnd":535,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/jpegtran.c#L499-L535","documentation":"jpegtran allocates a buffer for the ICC profile via malloc(icc_len) at jpegtran.c:516. If malloc returns NULL, the system cannot satisfy the allocation. The ICC profile size (determined by ftell) must be extremely large, or the process is under memory pressure. The program closes the ICC file handle and exits.","triggerScenarios":"ICC file is enormous (gigabytes) due to corruption or accidental use of a non-ICC file. The process has a low address-space limit (32-bit). System memory is exhausted or a malloc limit (setrlimit RLIMIT_AS) is in effect.","commonSituations":"Android JNI process under heavy memory load. The '-icc' flag accidentally points to a large file that is not an ICC profile. A 32-bit build where address space is limited to ~3GB.","solutions":["Sanity-check the ICC profile size before passing it: a real ICC profile is typically 500 bytes to a few MB; reject anything larger","Reduce concurrent memory usage in the calling process","Use a smaller ICC profile or an sRGB-compatible compressed profile"],"exampleFix":"// before\n// blindly pass any file as -icc\n\n// after\nstruct stat st;\nstat(icc_path, &st);\nif (st.st_size > 5*1024*1024) {\n    fprintf(stderr, \"ICC profile too large: %lld bytes\\n\", (long long)st.st_size);\n    return -1;\n}","handlingStrategy":"validation","validationCode":"// Reject abnormally large ICC files before processing\nint validate_icc_size(const char *path) {\n    struct stat st;\n    if (stat(path, &st) != 0) return -1;\n    // Real ICC profiles: 128 bytes minimum, typically under 1MB\n    if (st.st_size > 5*1024*1024) {\n        fprintf(stderr, \"ICC file too large: %lld bytes\\n\", (long long)st.st_size);\n        return -1;\n    }\n    return 0;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cap ICC profile size at a reasonable limit (5MB) before passing to jpegtran","Free memory in the calling process before invoking jpegtran with large ICC profiles","Use a known-good, compact sRGB ICC profile rather than large profiles"],"tags":["jpeg","jpegtran","icc-profile","memory","malloc"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}