DrKLO/Telegram · warning

Warning: garbage data found in JPEG file

Error message

Warning: garbage data found in JPEG file

What it means

The next_marker() function in rdjpgcom scans the byte stream for JPEG markers (0xFF followed by a non-zero marker code). If it encounters non-0xFF bytes before finding a marker, it counts them as 'discarded_bytes' and warns the user. This indicates the JPEG stream contains unexpected data between markers -- bytes that are not valid fill bytes or entropy-coded data. The function still continues parsing.

Source

Thrown at TMessagesProj/jni/mozjpeg/rdjpgcom.c:156

{
  int c;
  int discarded_bytes = 0;

  /* Find 0xFF byte; count and skip any non-FFs. */
  c = read_1_byte();
  while (c != 0xFF) {
    discarded_bytes++;
    c = read_1_byte();
  }
  /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
   * are legal as pad bytes, so don't count them in discarded_bytes.
   */
  do {
    c = read_1_byte();
  } while (c == 0xFF);

  if (discarded_bytes != 0) {
    fprintf(stderr, "Warning: garbage data found in JPEG file\n");
  }

  return c;
}


/*
 * Read the initial marker, which should be SOI.
 * For a JFIF file, the first two bytes of the file should be literally
 * 0xFF M_SOI.  To be more general, we could use next_marker, but if the
 * input file weren't actually JPEG at all, next_marker might read the whole
 * file and then return a misleading error message...
 */

static int
first_marker(void)
{
  int c1, c2;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Re-acquire the JPEG file from a trusted source to eliminate corruption.
  2. Use jpegtran or an external tool to re-encode the file, which normalizes the stream: jpegtran -copy none corrupt.jpg > clean.jpg.
  3. Inspect the file with a hex editor to locate and remove the garbage bytes.
  4. If the warning is benign (the file still decodes correctly), suppress stderr output if processing in a pipeline.

Example fix

# Re-encode to normalize the JPEG stream
jpegtran -copy none input.jpg > cleaned.jpg
rdjpgcom cleaned.jpg
Defensive patterns

Strategy: validation

Validate before calling

# Validate JPEG structure before processing
if ! jpegtran -copy none "$input" > /dev/null 2>&1; then
  echo "Warning: $input may be corrupted" >&2
fi

Prevention

When it happens

Trigger: Feeding a corrupted JPEG file to rdjpgcom; a JPEG file with garbage inserted between marker segments; a file that is mostly JPEG but has been concatenated with other data; a truncated file where partial marker data appears as garbage.

Common situations: Processing JPEG files that passed through unreliable storage or network transfers; files produced by buggy encoders that insert stray bytes; JPEG files recovered from damaged media; files that have been manipulated or have metadata appended incorrectly.

Related errors


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