{"record":{"id":"7cb609db4fb5d994","repo":"DrKLO/Telegram","slug":"s-memory-allocation-failure-n","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/djpeg.c","lineNumber":617,"sourceCode":"      exit(EXIT_FAILURE);\n    }\n  } else {\n    /* default output file is stdout */\n    output_file = write_stdout();\n  }\n\n#ifdef PROGRESS_REPORT\n  start_progress_monitor((j_common_ptr)&cinfo, &progress);\n#endif\n\n  /* Specify data source for decompression */\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(input_file, &inbuffer[insize], INPUT_BUF_SIZE);\n      if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {\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    fprintf(stderr, \"Compressed size:  %lu bytes\\n\", insize);\n    jpeg_mem_src(&cinfo, inbuffer, insize);\n  } else\n#endif\n    jpeg_stdio_src(&cinfo, input_file);\n","sourceCodeStart":599,"sourceCodeEnd":635,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/djpeg.c#L599-L635","documentation":"While loading the entire input into memory for the in-memory decompression source (-memsrc / memsrc mode), realloc failed to grow the read buffer by INPUT_BUF_SIZE. The program aborts immediately with exit(EXIT_FAILURE); no JPEG parsing has begun.","triggerScenarios":"Running djpeg with memory source mode on a very large input, or in a process whose memory ulimit/address space is constrained. The realloc loop grows the buffer in INPUT_BUF_SIZE chunks until EOF, so the failure reflects insufficient heap for the whole file.","commonSituations":"Mobile/embedded environments (Android NDK) with tight per-process memory caps, 32-bit address space exhaustion, ulimit -v set too low, OOM killer pressure, or attempting to memsrc-decode a multi-gigabyte JPEG.","solutions":["Avoid memsrc mode for large inputs; use stream source so data is buffered in chunks instead of fully resident.","Raise the process memory limit (ulimit -v / RLIMIT_AS) where the platform allows it.","Check available memory before invoking and reject inputs above a safe threshold.","Move to a 64-bit build if currently 32-bit."],"exampleFix":"// before: loads whole file into RAM\n./djpeg -memsrc huge.jpg > out.ppm\n// after: stream source, bounded memory\n./djpeg huge.jpg > out.ppm","handlingStrategy":"validation","validationCode":"#include <sys/stat.h>\n/* reject memsrc when file larger than a safe RAM budget */\nint memsrc_safe(const char *p, size_t budget) {\n  struct stat st;\n  if (stat(p, &st) != 0) return 0;\n  return (size_t)st.st_size <= budget;\n}\n/* e.g. budget = avail_phys_ram / 4 */","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid -memsrc for inputs above a size threshold.","Prefer streaming source to keep memory bounded.","Raise RLIMIT_AS only when the platform and process model allow it."],"tags":["memory","djpeg","memsrc","mozjpeg","oom"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}