{"record":{"id":"2a3971948e342513","repo":"DrKLO/Telegram","slug":"invalid-path-to-jpeg-file-n","errorCode":null,"errorMessage":"Invalid path to JPEG file!\\n","messagePattern":"Invalid path to JPEG file!\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/yuvjpeg.c","lineNumber":203,"sourceCode":"   malloc(frame_width*frame_height + 2*(frame_width/2)*(frame_height/2));\n  if (!jpg_buffer) {\n    free(yuv_buffer);\n    fprintf(stderr, \"Memory allocation failure!\\n\");\n    return 1;\n  }\n\n  extend_edge(jpg_buffer, frame_width, frame_height,\n   yuv_buffer, luma_width, luma_height, chroma_width, chroma_height);\n\n  free(yuv_buffer);\n\n  cinfo.err = jpeg_std_error(&jerr);\n  jpeg_create_compress(&cinfo);\n\n  jpg_fd = fopen(jpg_path, \"wb\");\n  if (!jpg_fd) {    \n    free(jpg_buffer);\n    fprintf(stderr, \"Invalid path to JPEG file!\\n\");\n    return 1;\n  }\n\n  jpeg_stdio_dest(&cinfo, jpg_fd);\n\n  cinfo.image_width = luma_width;\n  cinfo.image_height = luma_height;\n  cinfo.input_components = 3;\n\n  cinfo.in_color_space = JCS_YCbCr;\n  jpeg_set_defaults(&cinfo);\n\n  cinfo.raw_data_in = TRUE;\n\n  cinfo.comp_info[0].h_samp_factor = 2;\n  cinfo.comp_info[0].v_samp_factor = 2;\n  cinfo.comp_info[0].dc_tbl_no = 0;\n  cinfo.comp_info[0].ac_tbl_no = 0;","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/yuvjpeg.c#L185-L221","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the output directory exists and is writable (mkdir -p equivalent, access(..., W_OK)) before starting the conversion.","Request/check the appropriate Android storage permission before invoking the native converter.","Write to app-internal storage (getFilesDir()/getCacheDir()) which needs no runtime permission.","Add jpeg_destroy_compress(&cinfo) before returning to avoid leaking the libjpeg object on this path."],"exampleFix":"// before\njpg_fd = fopen(jpg_path, \"wb\");\nif (!jpg_fd) { free(jpg_buffer); fprintf(stderr, \"Invalid path to JPEG file!\\n\"); return 1; }\n\n// after: pre-validate dir + clean up libjpeg object\nif (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; }\njpg_fd = fopen(jpg_path, \"wb\");\nif (!jpg_fd) { free(jpg_buffer); jpeg_destroy_compress(&cinfo); fprintf(stderr, \"Invalid path to JPEG file!\\n\"); return 1; }","handlingStrategy":"validation","validationCode":"// Ensure the output directory is writable before starting the conversion.\nchar dir[PATH_MAX]; parent_dir_of(jpg_path, dir, sizeof dir);\nif (access(dir, W_OK) != 0) {\n    free(jpg_buffer);\n    fprintf(stderr, \"Output dir not writable: %s\\n\", dir);\n    return 1;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pre-validate the output directory with access(path, W_OK) and mkdir -p if needed.","Write to app-internal storage to avoid runtime permission requirements.","Always call jpeg_destroy_compress(&cinfo) on error paths to avoid leaking the libjpeg object."],"tags":["io","file-write","permissions","mozjpeg","jpeg","android-storage","c"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}