{"record":{"id":"4f102c083b547444","repo":"DrKLO/Telegram","slug":"error-writing-yuv-file","errorCode":null,"errorMessage":"Error writing yuv file\n","messagePattern":"Error writing yuv file\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/jpegyuv.c","lineNumber":165,"sourceCode":"         chroma_width*(chroma_scanline + y) + x] = crrow_pointer[y][x];\n      }\n    }\n  }\n\n  jpeg_finish_decompress(&cinfo);\n  jpeg_destroy_decompress(&cinfo);\n\n  fclose(jpg_fd);\n  free(jpg_buffer);\n\n  yuv_fd = fopen(yuv_path, \"wb\");\n  if (!yuv_fd) {\n    fprintf(stderr, \"Invalid path to YUV file!\");\n    free(yuv_buffer);\n    return 1;\n  }\n  if (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {\n    fprintf(stderr, \"Error writing yuv file\\n\");\n  }\n\n  fclose(yuv_fd);\n  free(yuv_buffer);\n\n  return 0;\n}\n","sourceCodeStart":147,"sourceCodeEnd":173,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/jpegyuv.c#L147-L173","documentation":"jpegyuv writes the YUV buffer with fwrite(yuv_buffer, yuv_size, 1, yuv_fd) at jpegyuv.c:164. If fwrite returns != 1, the full YUV data was not written due to disk full, I/O error, or quota. Notably, this error does NOT cause the program to return a failure code — it prints the message and continues to fclose and free, ultimately returning 0 (success). This is a bug: callers cannot detect the failure from the exit code.","triggerScenarios":"Disk fills up while writing the YUV output. I/O error on the output device. Quota exceeded mid-write. The output is on a FUSE filesystem that fails. The yuv_size is very large and the write is interrupted.","commonSituations":"Writing a large YUV file (multi-MB) to nearly-full storage. Android external storage unmounted during write. The output directory is on a failing SD card. The exit code 0 masks the failure from the calling process.","solutions":["Check available disk space before calling jpegyuv: ensure at least yuv_size bytes are free","Fix jpegyuv.c to return 1 after the fwrite failure so callers can detect it","After jpegyuv returns, verify the output file size matches the expected yuv_size","Write output to internal storage first for reliability"],"exampleFix":"// before (jpegyuv.c line 164-166)\nif (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {\n    fprintf(stderr, \"Error writing yuv file\\n\");\n}\n\n// after (fix the missing error return)\nif (fwrite(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {\n    fprintf(stderr, \"Error writing yuv file\\n\");\n    fclose(yuv_fd);\n    free(yuv_buffer);\n    return 1;\n}","handlingStrategy":"validation","validationCode":"// Check disk space and verify output after jpegyuv\nint verify_yuv_output(const char *yuv_path, long expected_size) {\n    struct stat st;\n    if (stat(yuv_path, &st) != 0) return -1;\n    if (st.st_size != expected_size) {\n        fprintf(stderr, \"YUV output incomplete: %lld vs expected %ld\\n\",\n                (long long)st.st_size, expected_size);\n        unlink(yuv_path);\n        return -1;\n    }\n    return 0;\n}\n// Also: patch jpegyuv.c to return 1 on fwrite failure (see exampleFix)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always verify output file size after jpegyuv returns, since it exits 0 even on write failure","Ensure sufficient disk space before writing YUV output","Fix jpegyuv.c to return a non-zero exit code on fwrite failure"],"tags":["jpeg","jpegyuv","file-io","write-error","disk-full","silent-failure"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}