DrKLO/Telegram · error

%s: memory allocation failure\n

Error message

%s: memory allocation failure\n

What it means

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.

Source

Thrown at TMessagesProj/jni/mozjpeg/djpeg.c:617

      exit(EXIT_FAILURE);
    }
  } else {
    /* default output file is stdout */
    output_file = write_stdout();
  }

#ifdef PROGRESS_REPORT
  start_progress_monitor((j_common_ptr)&cinfo, &progress);
#endif

  /* Specify data source for decompression */
#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
  if (memsrc) {
    size_t nbytes;
    do {
      inbuffer = (unsigned char *)realloc(inbuffer, insize + INPUT_BUF_SIZE);
      if (inbuffer == NULL) {
        fprintf(stderr, "%s: memory allocation failure\n", progname);
        exit(EXIT_FAILURE);
      }
      nbytes = JFREAD(input_file, &inbuffer[insize], INPUT_BUF_SIZE);
      if (nbytes < INPUT_BUF_SIZE && ferror(input_file)) {
        if (file_index < argc)
          fprintf(stderr, "%s: can't read from %s\n", progname,
                  argv[file_index]);
        else
          fprintf(stderr, "%s: can't read from stdin\n", progname);
      }
      insize += (unsigned long)nbytes;
    } while (nbytes == INPUT_BUF_SIZE);
    fprintf(stderr, "Compressed size:  %lu bytes\n", insize);
    jpeg_mem_src(&cinfo, inbuffer, insize);
  } else
#endif
    jpeg_stdio_src(&cinfo, input_file);

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Avoid memsrc mode for large inputs; use stream source so data is buffered in chunks instead of fully resident.
  2. Raise the process memory limit (ulimit -v / RLIMIT_AS) where the platform allows it.
  3. Check available memory before invoking and reject inputs above a safe threshold.
  4. Move to a 64-bit build if currently 32-bit.

Example fix

// before: loads whole file into RAM
./djpeg -memsrc huge.jpg > out.ppm
// after: stream source, bounded memory
./djpeg huge.jpg > out.ppm
Defensive patterns

Strategy: validation

Validate before calling

#include <sys/stat.h>
/* reject memsrc when file larger than a safe RAM budget */
int memsrc_safe(const char *p, size_t budget) {
  struct stat st;
  if (stat(p, &st) != 0) return 0;
  return (size_t)st.st_size <= budget;
}
/* e.g. budget = avail_phys_ram / 4 */

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/7cb609db4fb5d994. Report an issue: GitHub.