{"record":{"id":"8f45e76573e1e4f8","repo":"DrKLO/Telegram","slug":"s-memory-allocation-failure","errorCode":null,"errorMessage":"%s: memory allocation failure\n","messagePattern":"(.+?): memory allocation failure\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/jpegtran.c","lineNumber":548,"sourceCode":"      copyoption = JCOPYOPT_ALL_EXCEPT_ICC;\n  }\n\n#ifdef PROGRESS_REPORT\n  start_progress_monitor((j_common_ptr)&dstinfo, &progress);\n#endif\n\n  /* Specify data source for decompression */\n  if (jpeg_c_int_param_supported(&dstinfo, JINT_COMPRESS_PROFILE) &&\n      jpeg_c_get_int_param(&dstinfo, JINT_COMPRESS_PROFILE)\n        == JCP_MAX_COMPRESSION)\n    memsrc = TRUE; /* needed to revert to original */\n#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)\n  if (memsrc) {\n    size_t nbytes;\n    do {\n      inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);\n      if (inbuffer == NULL) {\n        fprintf(stderr, \"%s: memory allocation failure\\n\", progname);\n        exit(EXIT_FAILURE);\n      }\n      nbytes = JFREAD(fp, &inbuffer[insize], INPUT_BUF_SIZE);\n      if (nbytes < INPUT_BUF_SIZE && ferror(fp)) {\n        if (file_index < argc)\n          fprintf(stderr, \"%s: can't read from %s\\n\", progname,\n                  argv[file_index]);\n        else\n          fprintf(stderr, \"%s: can't read from stdin\\n\", progname);\n      }\n      insize += (unsigned long)nbytes;\n    } while (nbytes == INPUT_BUF_SIZE);\n    jpeg_mem_src(&srcinfo, inbuffer, insize);\n  } else\n#endif\n  jpeg_stdio_src(&srcinfo, fp);\n\n  /* Enable saving of extra markers that we want to copy */","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/jpegtran.c#L530-L566","documentation":"When JCP_MAX_COMPRESSION is active, jpegtran reads the entire input JPEG into a memory buffer via realloc in a grow-loop at jpegtran.c:546. If realloc returns NULL, the process cannot grow the buffer. This happens with very large JPEG files or under memory pressure. The program exits immediately.","triggerScenarios":"Processing a large JPEG (tens of MB+) with JCP_MAX_COMPRESSION enabled, which forces memsrc mode. Running on a device with limited RAM or a restrictive RLIMIT_AS. Multiple concurrent jpegtran instances competing for memory.","commonSituations":"Android device with limited heap. Processing a high-resolution photo (e.g. 50MP camera output). A build with JCP_MAX_COMPRESSION as the default profile. 32-bit build hitting address-space limits with a >2GB effective allocation.","solutions":["Disable JCP_MAX_COMPRESSION to use stdio source instead of memory source, avoiding the full-file buffer","Reduce the input JPEG dimensions before running jpegtran","Free other memory in the calling process before invoking jpegtran","Use a 64-bit build to increase address space"],"exampleFix":"// before\n// JCP_MAX_COMPRESSION forces entire file into memory\njpegtran -optimize -progressive large_photo.jpg -outfile out.jpg\n\n// after\n// disable max compression to stream via stdio\njpegtran -optimize large_photo.jpg -outfile out.jpg\n// or pre-resize the image\njpegtran -scale 1/2 large_photo.jpg -outfile smaller.jpg","handlingStrategy":"fallback","validationCode":"// Check available memory / file size before processing with max compression\nlong validate_memsrc_feasible(const char *input_path) {\n    struct stat st;\n    if (stat(input_path, &st) != 0) return -1;\n    // Need ~2x file size in memory for memsrc mode\n    long avail = get_available_memory(); // platform-specific\n    if ((unsigned long)st.st_size * 2 > (unsigned long)avail) {\n        fprintf(stderr, \"Insufficient memory for memsrc; file=%lld avail=%ld\\n\",\n                (long long)st.st_size, avail);\n        return -1;\n    }\n    return st.st_size;\n}","typeGuard":null,"tryCatchPattern":"// Shell fallback: try max compression, fall back to stdio\njpegtran -optimize input.jpg -outfile out.jpg 2>/dev/null\nif [ $? -ne 0 ]; then\n    echo \"Max compression failed, using stdio mode\" >&2\n    jpegtran input.jpg -outfile out.jpg\nfi","preventionTips":["Disable JCP_MAX_COMPRESSION for large files to avoid the in-memory buffer","Check file size vs available memory before processing","Process large images on a 64-bit build or with reduced concurrency"],"tags":["jpeg","jpegtran","memory","malloc","max-compression"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}