{"record":{"id":"4866a0a59033a221","repo":"wmjordan/PDFPatcher","slug":"malformed-exif-data","errorCode":null,"errorMessage":"Malformed Exif data","messagePattern":"Malformed Exif data","errorType":"exception","errorClass":"ExifLibException","httpStatus":null,"severity":"warning","filePath":"App/Processor/Imaging/JpgHelper.cs","lineNumber":412,"sourceCode":"\t\t\t\tif (markerStart != 0xFF || markerNumber != 0xE1)\r\n\t\t\t\t\tthrow new ExifLibException(\"Could not find Exif data block\");\r\n\t\t\t}\r\n\r\n\t\t\t/// <summary>\r\n\t\t\t/// Reads through the Exif data and builds an index of all Exif tags in the document\r\n\t\t\t/// </summary>\r\n\t\t\t/// <returns></returns>\r\n\t\t\tprivate void CreateTagIndex() {\r\n\t\t\t\t// The next 4 bytes are the size of the Exif data.\r\n\t\t\t\tReadUShort();\r\n\r\n\t\t\t\t// Next is the Exif data itself. It starts with the ASCII \"Exif\" followed by 2 zero bytes.\r\n\t\t\t\tif (ReadString(4) != \"Exif\")\r\n\t\t\t\t\tthrow new ExifLibException(\"Exif data not found\");\r\n\r\n\t\t\t\t// 2 zero bytes\r\n\t\t\t\tif (ReadUShort() != 0)\r\n\t\t\t\t\tthrow new ExifLibException(\"Malformed Exif data\");\r\n\r\n\t\t\t\t// We're now into the TIFF format\r\n\t\t\t\t_tiffHeaderStart = _stream.Position;\r\n\r\n\t\t\t\t// What byte align will be used for the TIFF part of the document? II for Intel, MM for Motorola\r\n\t\t\t\t_isLittleEndian = ReadString(2) == \"II\";\r\n\r\n\t\t\t\t// Next 2 bytes are always the same.\r\n\t\t\t\tif (ReadUShort() != 0x002A)\r\n\t\t\t\t\tthrow new ExifLibException(\"Error in TIFF data\");\r\n\r\n\t\t\t\t// Get the offset to the IFD (image file directory)\r\n\t\t\t\tvar ifdOffset = ReadUint();\r\n\r\n\t\t\t\t// Note that this offset is from the first byte of the TIFF header. Jump to the IFD.\r\n\t\t\t\t_stream.Position = ifdOffset + _tiffHeaderStart;\r\n\r\n\t\t\t\t// Catalogue this first IFD (there will be another IFD)\r","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/Imaging/JpgHelper.cs#L394-L430","documentation":"JpgHelper.CreateTagIndex throws ExifLibException('Malformed Exif data') when the 2 bytes immediately after the 'Exif' signature are not zero. Per the Exif spec these two padding bytes must be 0x0000; a non-zero value indicates the Exif header is corrupt or the segment was truncated/misaligned.","triggerScenarios":"A JPEG whose APP1 Exif segment has been corrupted, truncated, or misaligned such that the 2-byte padding after 'Exif' is non-zero. This points to structural damage in the Exif block rather than missing metadata.","commonSituations":"A JPEG damaged in transit/storage; an Exif block written by buggy encoding software; manual editing of the JPEG that shifted bytes; a truncated download.","solutions":["Catch ExifLibException and degrade gracefully to 'no Exif' for corrupt segments.","Validate the Exif segment structure before deep parsing; abort early on the first integrity check failure.","Re-encode or repair the JPEG with a reliable image tool to regenerate a clean Exif block.","Skip the corrupt file in batch processing and log it for later repair."],"exampleFix":"// before\nif (ReadUShort() != 0)\n  throw new ExifLibException(\"Malformed Exif data\");\n\n// after\nif (ReadUShort() != 0) {\n  Tracker.TraceMessage(Tracker.Category.Warning, \"Exif 数据损坏，跳过。\");\n  _ifd0Catalogue = new Dictionary<ushort, long>();\n  return;\n}","handlingStrategy":"try-catch","validationCode":"// Structural integrity cannot be pre-validated cheaply; rely on catch.","typeGuard":null,"tryCatchPattern":"try { CreateTagIndex(); }\ncatch (ExifLibException) { _ifd0Catalogue = new Dictionary<ushort, long>(); }","preventionTips":["Catch ExifLibException for corrupt Exif segments and continue.","Repair corrupt JPEGs with an Exif-aware tool.","Log corrupt files during batch runs."],"tags":["image","jpeg","exif","parsing","corrupt-file"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}