DrKLO/Telegram · error

Invalid path to JPEG file!\n

Error message

Invalid path to JPEG file!\n

What it means

Printed when fopen(jpg_path, "wb") returns NULL — the destination JPEG file cannot be opened for writing. The function frees jpg_buffer and returns 1. Note jpeg_create_compress(&cinfo) has already run by this point, so the libjpeg compress object is leaked on this error path (no jpeg_destroy_compress).

Source

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

   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);

  free(yuv_buffer);

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

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

  jpeg_stdio_dest(&cinfo, jpg_fd);

  cinfo.image_width = luma_width;
  cinfo.image_height = luma_height;
  cinfo.input_components = 3;

  cinfo.in_color_space = JCS_YCbCr;
  jpeg_set_defaults(&cinfo);

  cinfo.raw_data_in = TRUE;

  cinfo.comp_info[0].h_samp_factor = 2;
  cinfo.comp_info[0].v_samp_factor = 2;
  cinfo.comp_info[0].dc_tbl_no = 0;
  cinfo.comp_info[0].ac_tbl_no = 0;

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Verify the output directory exists and is writable (mkdir -p equivalent, access(..., W_OK)) before starting the conversion.
  2. Request/check the appropriate Android storage permission before invoking the native converter.
  3. Write to app-internal storage (getFilesDir()/getCacheDir()) which needs no runtime permission.
  4. Add jpeg_destroy_compress(&cinfo) before returning to avoid leaking the libjpeg object on this path.

Example fix

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

// after: pre-validate dir + clean up libjpeg object
if (access_parent_dir(jpg_path, W_OK) != 0) { free(jpg_buffer); jpeg_destroy_compress(&cinfo); fprintf(stderr, "Invalid path to JPEG file!\n"); return 1; }
jpg_fd = fopen(jpg_path, "wb");
if (!jpg_fd) { free(jpg_buffer); jpeg_destroy_compress(&cinfo); fprintf(stderr, "Invalid path to JPEG file!\n"); return 1; }
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the output directory is writable before starting the conversion.
char dir[PATH_MAX]; parent_dir_of(jpg_path, dir, sizeof dir);
if (access(dir, W_OK) != 0) {
    free(jpg_buffer);
    fprintf(stderr, "Output dir not writable: %s\n", dir);
    return 1;
}

Prevention

When it happens

Trigger: fopen of the output path in write-binary mode fails: nonexistent directory, permission denied, read-only filesystem, path too long, or disk full. The YUV->JPEG conversion has already done all the heavy work (read, edge-extend) before this point.

Common situations: Android external storage path that lacks WRITE_EXTERNAL_STORAGE permission; a path whose parent directory was never mkdir'd; writing to a path on a mounted-read-only sdcard; the app's cache dir was cleared mid-operation; encoding to a Content URI-backed fd without a real filesystem path.

Related errors


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