{"record":{"id":"4e393ce43ce96126","repo":"DrKLO/Telegram","slug":"s-can-t-open-stdout-n","errorCode":null,"errorMessage":"%s: can't open stdout\\n","messagePattern":"(.+?): can't open stdout\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/wrjpgcom.c","lineNumber":536,"sourceCode":"    usage();\n  }\n  if ((outfile = fopen(argv[argn + 1], WRITE_BINARY)) == NULL) {\n    fprintf(stderr, \"%s: can't open %s\\n\", progname, argv[argn + 1]);\n    exit(EXIT_FAILURE);\n  }\n#else\n  /* Unix style: expect zero or one file name */\n  if (argn < argc - 1) {\n    fprintf(stderr, \"%s: only one input file\\n\", progname);\n    usage();\n  }\n  /* default output file is stdout */\n#ifdef USE_SETMODE              /* need to hack file mode? */\n  setmode(fileno(stdout), O_BINARY);\n#endif\n#ifdef USE_FDOPEN               /* need to re-open in binary mode? */\n  if ((outfile = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {\n    fprintf(stderr, \"%s: can't open stdout\\n\", progname);\n    exit(EXIT_FAILURE);\n  }\n#else\n  outfile = stdout;\n#endif\n#endif /* TWO_FILE_COMMANDLINE */\n\n  /* Collect comment text from comment_file or stdin, if necessary */\n  if (comment_arg == NULL) {\n    FILE *src_file;\n    int c;\n\n    comment_arg = (char *)malloc((size_t)MAX_COM_LENGTH);\n    if (comment_arg == NULL)\n      ERREXIT(\"Insufficient memory\");\n    comment_length = 0;\n    src_file = (comment_file != NULL ? comment_file : stdin);\n    while ((c = getc(src_file)) != EOF) {","sourceCodeStart":518,"sourceCodeEnd":554,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/wrjpgcom.c#L518-L554","documentation":"In the Unix-style build with USE_FDOPEN, when no output file is named wrjpgcom writes to stdout and re-opens fd 1 in binary mode via fdopen(fileno(stdout), WRITE_BINARY). If fdopen returns NULL it prints `%s: can't open stdout` and exits EXIT_FAILURE. This is a stdout re-attach failure, not a disk error.","triggerScenarios":"Running wrjpgcom with stdout closed or an invalid fd 1 (e.g. launched with stdout>&- or from a process that closed fd 1), under a USE_FDOPEN build.","commonSituations":"Daemon/JNI spawn that did not wire stdout, `>&-` redirection in a shell wrapper, or piping into a command that immediately closes its stdin.","solutions":["Redirect stdout to a real file: `wrjpgcom ... > out.jpg`.","Use a TWO_FILE_COMMANDLINE build and name an explicit output file.","Ensure fd 1 is a valid open writable descriptor before launching."],"exampleFix":"// before\nwrjpgcom -comment hi in.jpg   # stdout closed\n// after\nwrjpgcom -comment hi in.jpg > out.jpg","handlingStrategy":"validation","validationCode":"#include <unistd.h>\n/* ensure stdout (fd 1) is a valid writable descriptor */\nif (fcntl(1, F_GETFD) == -1) { /* redirect or name an output file */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Redirect stdout to a file: wrjpgcom ... > out.jpg.","Use a TWO_FILE_COMMANDLINE build with a named output.","Never launch with fd 1 closed."],"tags":["cli","jpeg","mozjpeg","wrjpgcom","stdio","stdout","environment"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}