{"record":{"id":"b090c1543c735dbd","repo":"DrKLO/Telegram","slug":"s-can-t-allocate-memory-for-icc-profile","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/cjpeg.c","lineNumber":774,"sourceCode":"  } else if (!memdst) {\n    /* default output file is stdout */\n    output_file = write_stdout();\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  }\n\n#ifdef PROGRESS_REPORT\n  start_progress_monitor((j_common_ptr)&cinfo, &progress);\n#endif\n\n  /* Figure out the input file format, and set up to read it. */","sourceCodeStart":756,"sourceCodeEnd":792,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/cjpeg.c#L756-L792","documentation":"cjpeg allocates a buffer of icc_len bytes with malloc to hold the ICC profile. If malloc returns NULL (insufficient memory, or icc_len was absurdly large due to a corrupt size), it prints this message, closes the file, and exits EXIT_FAILURE.","triggerScenarios":"The system cannot satisfy the malloc(icc_len) request: genuine out-of-memory, a malformed file whose ftell reported an enormous length, or ulimit/cgroup memory caps on the process.","commonSituations":"Embedded/mobile environments with tight memory limits; a corrupted file causing ftell to report a multi-GB size; many concurrent encodings exhausting RAM; a path resolution bug pointing at a huge unrelated file.","solutions":["Free memory before encoding or reduce concurrency; raise the process memory ulimit if appropriate.","Sanity-check the reported ICC size (a real ICC profile is typically a few KB to a few MB) and reject implausibly large files before passing -icc.","Verify the file is actually an ICC profile and not a large unrelated binary."],"exampleFix":"# before: huge/corrupt file exhausts malloc\ncjpeg -icc huge.bin in.ppm out.jpg\n# after: validate size first\nsize=$(stat -c%s profile.icc); [ \"$size\" -lt 1048576 ] && cjpeg -icc profile.icc in.ppm out.jpg","handlingStrategy":"validation","validationCode":"#!/bin/sh\nICC=\"$1\"; MAX=$((2*1024*1024))  # ICC profiles are at most a few MB\nsize=$(stat -c%s \"$ICC\" 2>/dev/null || stat -f%z \"$ICC\")\n[ \"$size\" -ge 1 ] 2>/dev/null && [ \"$size\" -le \"$MAX\" ] || { echo \"ICC size implausible ($size): $ICC\" >&2; exit 1; }\ncjpeg -icc \"$ICC\" in.ppm out.jpg","typeGuard":"// n/a: resource precondition check in the caller.","tryCatchPattern":"if ! cjpeg -icc \"$ICC\" in.ppm out.jpg 2>/tmp/e; then\n  grep -q 'allocate memory for ICC' /tmp/e && { echo 'OOM: reduce concurrency or shrink/recheck ICC' >&2; exit 1; }\nfi","preventionTips":["Cap and validate ICC file size before allocation (typical profiles < 1 MB).","Limit concurrent encoding jobs on memory-constrained devices.","Raise ulimit -v / cgroup memory limits if legitimately large profiles are used."],"tags":["mozjpeg","cjpeg","memory","malloc","icc-profile"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}