{"record":{"id":"56b2466e10476285","repo":"DrKLO/Telegram","slug":"error-reading-yuv-file-n","errorCode":null,"errorMessage":"Error reading yuv file\\n","messagePattern":"Error reading yuv file\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"TMessagesProj/jni/mozjpeg/yuvjpeg.c","lineNumber":176,"sourceCode":"  fseek(yuv_fd, 0, SEEK_SET);\n\n  /* Check that the file size matches 4:2:0 yuv. */\n  if (yuv_size !=\n   (size_t)luma_width*luma_height + 2*chroma_width*chroma_height) {\n    fclose(yuv_fd);\n    fprintf(stderr, \"Unexpected input format!\\n\");\n    return 1;\n  }\n\n  yuv_buffer = malloc(yuv_size);\n  if (!yuv_buffer) {\n    fclose(yuv_fd);\n    fprintf(stderr, \"Memory allocation failure!\\n\");\n    return 1;\n  }\n\n  if (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {\n    fprintf(stderr, \"Error reading yuv file\\n\");\n  };\n\n  fclose(yuv_fd);\n\n  frame_width = (luma_width + (16 - 1)) & ~(16 - 1);\n  frame_height = (luma_height + (16 - 1)) & ~(16 - 1);\n\n  jpg_buffer =\n   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","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/jni/mozjpeg/yuvjpeg.c#L158-L194","documentation":"Printed when fread() cannot read exactly one element of yuv_size bytes from the YUV file into yuv_buffer. Unlike the malloc failure, this message is NOT fatal — the code prints the error but continues executing (no return), so it proceeds to use a partially-filled buffer. This is a latent bug: yuv_buffer may contain uninitialized data for the unread portion.","triggerScenarios":"fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1. Happens when the file is truncated relative to the size the header claimed, when a stream/pipe ends early, on I/O errors, or when the file descriptor position is wrong because fseek/ftell earlier computed yuv_size incorrectly.","commonSituations":"Corrupt or partially-downloaded YUV file whose actual byte count is less than luma+chroma planes; passing a pipe/socket instead of a regular file; concurrent truncation of the file while reading; mismatch between the reported resolution and the real data length.","solutions":["Validate the YUV file's actual byte size (via fseek/ftell or fstat) equals yuv_size before allocating and reading.","Make the error fatal by returning 1 after the fprintf, so the partially-filled buffer is never used.","Re-acquire or re-download the source YUV if it is truncated.","Ensure the file position is at the start of frame data before fread (no prior reads shifting the offset)."],"exampleFix":"// before\nif (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {\n    fprintf(stderr, \"Error reading yuv file\\n\");\n};\n\n// after: fail hard and free resources\nif (fread(yuv_buffer, yuv_size, 1, yuv_fd) != 1) {\n    fprintf(stderr, \"Error reading yuv file\\n\");\n    free(yuv_buffer);\n    fclose(yuv_fd);\n    return 1;\n}","handlingStrategy":"validation","validationCode":"// Confirm the file actually contains yuv_size bytes before reading.\nstruct stat st;\nif (fstat(fileno(yuv_fd), &st) != 0 || (size_t)st.st_size < yuv_size) {\n    fclose(yuv_fd);\n    fprintf(stderr, \"YUV file too short (%lld < %zu)\\n\", (long long)st.st_size, yuv_size);\n    return 1;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Stat the file to verify its byte length matches the expected 4:2:0 size before allocating.","Make a read failure fatal (free + return) so a partially-filled buffer is never used downstream.","Use regular files rather than pipes/sockets for whole-frame reads."],"tags":["io","file-read","mozjpeg","yuv","data-integrity","c"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}