wmjordan/PDFPatcher · warning · ExifLibException

Malformed Exif data

Error message

Malformed Exif data

What it means

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.

Source

Thrown at App/Processor/Imaging/JpgHelper.cs:412

				if (markerStart != 0xFF || markerNumber != 0xE1)
					throw new ExifLibException("Could not find Exif data block");
			}

			/// <summary>
			/// Reads through the Exif data and builds an index of all Exif tags in the document
			/// </summary>
			/// <returns></returns>
			private void CreateTagIndex() {
				// The next 4 bytes are the size of the Exif data.
				ReadUShort();

				// Next is the Exif data itself. It starts with the ASCII "Exif" followed by 2 zero bytes.
				if (ReadString(4) != "Exif")
					throw new ExifLibException("Exif data not found");

				// 2 zero bytes
				if (ReadUShort() != 0)
					throw new ExifLibException("Malformed Exif data");

				// We're now into the TIFF format
				_tiffHeaderStart = _stream.Position;

				// What byte align will be used for the TIFF part of the document? II for Intel, MM for Motorola
				_isLittleEndian = ReadString(2) == "II";

				// Next 2 bytes are always the same.
				if (ReadUShort() != 0x002A)
					throw new ExifLibException("Error in TIFF data");

				// Get the offset to the IFD (image file directory)
				var ifdOffset = ReadUint();

				// Note that this offset is from the first byte of the TIFF header. Jump to the IFD.
				_stream.Position = ifdOffset + _tiffHeaderStart;

				// Catalogue this first IFD (there will be another IFD)

View on GitHub (pinned to 4782bbd9ad)

Solutions

  1. Catch ExifLibException and degrade gracefully to 'no Exif' for corrupt segments.
  2. Validate the Exif segment structure before deep parsing; abort early on the first integrity check failure.
  3. Re-encode or repair the JPEG with a reliable image tool to regenerate a clean Exif block.
  4. Skip the corrupt file in batch processing and log it for later repair.

Example fix

// before
if (ReadUShort() != 0)
  throw new ExifLibException("Malformed Exif data");

// after
if (ReadUShort() != 0) {
  Tracker.TraceMessage(Tracker.Category.Warning, "Exif 数据损坏,跳过。");
  _ifd0Catalogue = new Dictionary<ushort, long>();
  return;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Structural integrity cannot be pre-validated cheaply; rely on catch.

Try / catch

try { CreateTagIndex(); }
catch (ExifLibException) { _ifd0Catalogue = new Dictionary<ushort, long>(); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13). Data as JSON: /api/errors/4866a0a59033a221. Report an issue: GitHub.