DrKLO/Telegram · error

Error reading yuv file\n

Error message

Error reading yuv file\n

What it means

Printed when fread() cannot read exactly one element of yuv_size bytes from the YUV file into yuv_buffer. Unlike the malloc failure, this message is NOT fatal — the code prints the error but continues executing (no return), so it proceeds to use a partially-filled buffer. This is a latent bug: yuv_buffer may contain uninitialized data for the unread portion.

Source

Thrown at TMessagesProj/jni/mozjpeg/yuvjpeg.c:176

  fseek(yuv_fd, 0, SEEK_SET);

  /* Check that the file size matches 4:2:0 yuv. */
  if (yuv_size !=
   (size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {
    fclose(yuv_fd);
    fprintf(stderr, "Unexpected input format!\n");
    return 1;
  }

  yuv_buffer = malloc(yuv_size);
  if (!yuv_buffer) {
    fclose(yuv_fd);
    fprintf(stderr, "Memory allocation failure!\n");
    return 1;
  }

  if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error reading yuv file\n");
  };

  fclose(yuv_fd);

  frame_width = (luma_width + (16 - 1)) & ~(16 - 1);
  frame_height = (luma_height + (16 - 1)) & ~(16 - 1);

  jpg_buffer =
   malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2));
  if (!jpg_buffer) {
    free(yuv_buffer);
    fprintf(stderr, "Memory allocation failure!\n");
    return 1;
  }

  extend_edge(jpg_buffer, frame_width, frame_height,
   yuv_buffer, luma_width, luma_height, chroma_width, chroma_height);

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Validate the YUV file's actual byte size (via fseek/ftell or fstat) equals yuv_size before allocating and reading.
  2. Make the error fatal by returning 1 after the fprintf, so the partially-filled buffer is never used.
  3. Re-acquire or re-download the source YUV if it is truncated.
  4. Ensure the file position is at the start of frame data before fread (no prior reads shifting the offset).

Example fix

// before
if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error reading yuv file\n");
};

// after: fail hard and free resources
if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error reading yuv file\n");
    free(yuv_buffer);
    fclose(yuv_fd);
    return 1;
}
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the file actually contains yuv_size bytes before reading.
struct stat st;
if (fstat(fileno(yuv_fd), &st) != 0 || (size_t)st.st_size < yuv_size) {
    fclose(yuv_fd);
    fprintf(stderr, "YUV file too short (%lld < %zu)\n", (long long)st.st_size, yuv_size);
    return 1;
}

Prevention

When it happens

Trigger: fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1. Happens when the file is truncated relative to the size the header claimed, when a stream/pipe ends early, on I/O errors, or when the file descriptor position is wrong because fseek/ftell earlier computed yuv_size incorrectly.

Common situations: Corrupt or partially-downloaded YUV file whose actual byte count is less than luma+chroma planes; passing a pipe/socket instead of a regular file; concurrent truncation of the file while reading; mismatch between the reported resolution and the real data length.

Related errors


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