DrKLO/Telegram · error

Error writing yuv file

Error message

Error writing yuv file

What it means

jpegyuv writes the YUV buffer with fwrite(yuv_buffer, yuv_size, 1, yuv_fd) at jpegyuv.c:164. If fwrite returns != 1, the full YUV data was not written due to disk full, I/O error, or quota. Notably, this error does NOT cause the program to return a failure code — it prints the message and continues to fclose and free, ultimately returning 0 (success). This is a bug: callers cannot detect the failure from the exit code.

Source

Thrown at TMessagesProj/jni/mozjpeg/jpegyuv.c:165

         chroma_width*(chroma_scanline + y) + x] = crrow_pointer[y][x];
      }
    }
  }

  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);

  fclose(jpg_fd);
  free(jpg_buffer);

  yuv_fd = fopen(yuv_path, "wb");
  if (!yuv_fd) {
    fprintf(stderr, "Invalid path to YUV file!");
    free(yuv_buffer);
    return 1;
  }
  if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error writing yuv file\n");
  }

  fclose(yuv_fd);
  free(yuv_buffer);

  return 0;
}

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Check available disk space before calling jpegyuv: ensure at least yuv_size bytes are free
  2. Fix jpegyuv.c to return 1 after the fwrite failure so callers can detect it
  3. After jpegyuv returns, verify the output file size matches the expected yuv_size
  4. Write output to internal storage first for reliability

Example fix

// before (jpegyuv.c line 164-166)
if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error writing yuv file\n");
}

// after (fix the missing error return)
if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {
    fprintf(stderr, "Error writing yuv file\n");
    fclose(yuv_fd);
    free(yuv_buffer);
    return 1;
}
Defensive patterns

Strategy: validation

Validate before calling

// Check disk space and verify output after jpegyuv
int verify_yuv_output(const char *yuv_path, long expected_size) {
    struct stat st;
    if (stat(yuv_path, &st) != 0) return -1;
    if (st.st_size != expected_size) {
        fprintf(stderr, "YUV output incomplete: %lld vs expected %ld\n",
                (long long)st.st_size, expected_size);
        unlink(yuv_path);
        return -1;
    }
    return 0;
}
// Also: patch jpegyuv.c to return 1 on fwrite failure (see exampleFix)

Prevention

When it happens

Trigger: Disk fills up while writing the YUV output. I/O error on the output device. Quota exceeded mid-write. The output is on a FUSE filesystem that fails. The yuv_size is very large and the write is interrupted.

Common situations: Writing a large YUV file (multi-MB) to nearly-full storage. Android external storage unmounted during write. The output directory is on a failing SD card. The exit code 0 masks the failure from the calling process.

Related errors


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