DrKLO/Telegram · error

Invalid path to JPEG file!

Error message

Invalid path to JPEG file!

What it means

jpegyuv opens the JPEG input with fopen(jpg_path, "rb") at jpegyuv.c:77. If fopen returns NULL, the path is invalid: the file doesn't exist, isn't readable, or the path is malformed. The program prints 'Invalid path to JPEG file!' and returns 1 without processing.

Source

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

  FILE *yuv_fd;

  if (argc != 3) {
    fprintf(stderr, "Required arguments:\n");
    fprintf(stderr, "1. Path to JPG input file\n");
    fprintf(stderr, "2. Path to YUV output file\n");
    return 1;
  }

  /* Will check these for validity when opening via 'fopen'. */
  jpg_path = argv[1];
  yuv_path = argv[2];

  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_decompress(&cinfo);

  jpg_fd = fopen(jpg_path, "rb");
  if (!jpg_fd) {
    fprintf(stderr, "Invalid path to JPEG file!\n");
    return 1;
  }

  jpeg_stdio_src(&cinfo, jpg_fd);

  jpeg_read_header(&cinfo, TRUE);

  cinfo.raw_data_out = TRUE;
  cinfo.do_fancy_upsampling = FALSE;

  jpeg_start_decompress(&cinfo);

  luma_width = cinfo.output_width;
  luma_height = cinfo.output_height;

  chroma_width = (luma_width + 1) >> 1;
  chroma_height = (luma_height + 1) >> 1;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Verify the file exists and is readable: `access(jpg_path, R_OK) == 0` before invoking
  2. Use absolute paths, especially from JNI where cwd may not match expectations
  3. On Android, extract files from content URIs to internal storage first

Example fix

// before (Java/JNI)
nativeConvertJpegToYuv(contentUri.getPath(), yuvPath);

// after
File tmp = new File(context.getFilesDir(), "input.jpg");
copyUriToFile(contentUri, tmp);
nativeConvertJpegToYuv(tmp.getAbsolutePath(), yuvPath);
Defensive patterns

Strategy: validation

Validate before calling

// Validate JPEG input file before calling jpegyuv
int validate_jpeg_for_yuv(const char *path) {
    if (path == NULL || path[0] == '\0') return -1;
    if (access(path, R_OK) != 0) return -1;
    struct stat st;
    if (stat(path, &st) != 0) return -1;
    if (!S_ISREG(st.st_mode)) return -1;
    if (st.st_size < 3) return -1; // too small for JPEG (FFD8 minimum)
    return 0;
}

Prevention

When it happens

Trigger: The JPEG file path doesn't exist on the filesystem. The path is a directory. The file has restrictive permissions. A relative path that resolves against the wrong working directory in JNI.

Common situations: Android JNI caller passes a path from Java that doesn't resolve on the native filesystem (e.g. a content:// URI instead of a real filesystem path). Path has trailing whitespace or a newline. File was deleted between path resolution and fopen.

Related errors


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