{"record":{"id":"5c442c5f6de5c50f","repo":"DrKLO/Telegram","slug":"s-can-t-open-s-for-reading","errorCode":null,"errorMessage":"%s: can't open %s for reading\n","messagePattern":"(.+?): can't open (.+?) for reading\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/jpegtran.c","lineNumber":495,"sourceCode":"  } else {\n    if (file_index != argc - 1) {\n      fprintf(stderr, \"%s: must name one input and one output file\\n\",\n              progname);\n      usage();\n    }\n  }\n#else\n  /* Unix style: expect zero or one file name */\n  if (file_index < argc - 1) {\n    fprintf(stderr, \"%s: only one input file\\n\", progname);\n    usage();\n  }\n#endif /* TWO_FILE_COMMANDLINE */\n\n  /* Open the input file. */\n  if (file_index < argc) {\n    if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {\n      fprintf(stderr, \"%s: can't open %s for reading\\n\", progname,\n              argv[file_index]);\n      exit(EXIT_FAILURE);\n    }\n  } else {\n    /* default input file is stdin */\n    fp = read_stdin();\n  }\n\n  if (icc_filename != NULL) {\n    if ((icc_file = fopen(icc_filename, READ_BINARY)) == NULL) {\n      fprintf(stderr, \"%s: can't open %s\\n\", progname, icc_filename);\n      exit(EXIT_FAILURE);\n    }\n    if (fseek(icc_file, 0, SEEK_END) < 0 ||\n        (icc_len = ftell(icc_file)) < 1 ||\n        fseek(icc_file, 0, SEEK_SET) < 0) {\n      fprintf(stderr, \"%s: can't determine size of %s\\n\", progname,\n              icc_filename);","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/jpegtran.c#L477-L513","documentation":"jpegtran calls fopen(argv[file_index], READ_BINARY) to open the input JPEG at jpegtran.c:494. If fopen returns NULL, the file does not exist, is not readable by the current user, or the path is malformed. The program prints the error and immediately calls exit(EXIT_FAILURE).","triggerScenarios":"Passing a non-existent file path, a path to a directory instead of a JPEG, a file with restrictive permissions (mode 000 or owned by another user), or a path containing a typo or stale reference.","commonSituations":"Hardcoded path in a JNI wrapper points to a file that was deleted or moved. Path with spaces not quoted in a shell invocation. File is in an Android content URI or sandboxed directory inaccessible to the native process. A relative path resolves against the wrong working directory.","solutions":["Verify the file exists and is readable before invoking: `access(path, R_OK) == 0`","Use an absolute path, not a relative one, especially when calling from JNI where cwd may differ","Check file permissions: `ls -l <path>` and ensure the calling process has read access","If on Android, copy the file to the app's internal storage (`getFilesDir()`) and pass that path to jpegtran"],"exampleFix":"// before (C caller)\nint ret = system(\"jpegtran /data/media/photo.jpg -outfile out.jpg\");\n\n// after\nconst char *path = \"/data/media/photo.jpg\";\nif (access(path, R_OK) != 0) {\n    fprintf(stderr, \"input not readable: %s\\n\", strerror(errno));\n    return -1;\n}\nint ret = system(\"jpegtran /data/media/photo.jpg -outfile out.jpg\");","handlingStrategy":"validation","validationCode":"#include <unistd.h>\n// validate input file before invoking jpegtran\nint validate_jpeg_input(const char *path) {\n    if (access(path, R_OK) != 0) return -1;\n    struct stat st;\n    if (stat(path, &st) != 0) return -1;\n    if (!S_ISREG(st.st_mode)) return -1;\n    if (st.st_size < 2) return -1; // too small to be a JPEG\n    return 0;\n}","typeGuard":null,"tryCatchPattern":"// Shell wrapper: check exit code after jpegtran\njpegtran \"$input\" -outfile \"$output\"\nrc=$?\nif [ $rc -ne 0 ]; then\n    echo \"jpegtran failed (exit $rc)\" >&2\n    exit $rc\nfi","preventionTips":["Always use absolute paths for input files in JNI contexts","Call access(path, R_OK) before invoking jpegtran","On Android, copy files from content URIs to internal storage before processing"],"tags":["jpeg","jpegtran","file-io","permissions"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}