Index: src/Imaging/Tiff.cs
===================================================================
--- src/Imaging/Tiff.cs	(revision 4686)
+++ src/Imaging/Tiff.cs	(working copy)
@@ -914,7 +914,7 @@
 #if DEBUG_LOADER
 			System.Console.WriteLine ("Reading First IFD");
 #endif
-			Directory = new ImageDirectory (stream, directory_offset, endian); 
+			Directory = new ImageDirectory (stream, directory_offset, endian, 0); 
 			//}
 		}
 		
@@ -1125,20 +1125,22 @@
 		protected ushort num_entries;
 		protected List<DirectoryEntry> entries;
 		protected uint orig_position;
+		protected uint directory_depth; // to detect recursive IFDs
 
 		protected uint next_directory_offset;
 		ImageDirectory next_directory;
 		
 		protected bool has_header;
 		protected bool has_footer;
-
-		public ImageDirectory (System.IO.Stream stream, uint start_position, Endian endian)
+		
+		public ImageDirectory (System.IO.Stream stream, uint start_position, Endian endian, uint depth)
 		{
 			this.endian = endian;
 			orig_position = start_position;
+			directory_depth = depth;
 			Load (stream);
 		}
-		
+
 		public uint Save (OrderedWriter writer, uint position)
 		{
 			writer.Write (position);
@@ -1230,7 +1232,7 @@
 			next_directory = null;
 			try {
 				if (next_directory_offset != 0 && next_directory_offset != orig_position)
-					next_directory = new ImageDirectory (stream, next_directory_offset, this.endian);
+					next_directory = new ImageDirectory (stream, next_directory_offset, this.endian, directory_depth);
 				
 			} catch (System.Exception) {
 				//System.Console.WriteLine ("Error loading directory {0}", e.ToString ());
@@ -1294,19 +1296,19 @@
 			case TagId.GPSInfoIfdPointer:
 			case TagId.InteroperabilityIfdPointer:
 			case TagId.SubIFDs:
-				return new SubdirectoryEntry (input, start, header_endian);
+				return new SubdirectoryEntry (input, start, header_endian, parent.directory_depth);
 				//case TagId.MakerNote:
-				//return new MakerNoteEntry (input, start, header_endian);
+				//return new MakerNoteEntry (input, start, header_endian, parent.directory_depth);
 				//case TagId.PimIfdPointer:
 				//return new 
 				//case TagId.MakerNote:
-				//return new MakerNoteEntry (input, start, header_endian);
+				//return new MakerNoteEntry (input, start, header_endian, parent.directory_depth);
 			}
 			
 			switch (type) {
 			case EntryType.Ifd:
 				//System.Console.WriteLine ("Trying to load {0} {1}", tagid, tagid.ToString ("x"));
-				return new SubdirectoryEntry (input, start, header_endian);
+				return new SubdirectoryEntry (input, start, header_endian, parent.directory_depth);
 			case EntryType.Byte:
 				return new ByteEntry (input, start, header_endian);
 			case EntryType.Long:
@@ -1434,7 +1436,8 @@
 	}
 	
 	public class MakerNoteEntry : SubdirectoryEntry {
-		public MakerNoteEntry (byte [] data, int offset, Endian endian) : base (data, offset, endian)
+		public MakerNoteEntry (byte [] data, int offset, Endian endian, uint depth)
+			: base (data, offset, endian, depth)
 		{
 		
 		}
@@ -1451,11 +1454,16 @@
 
 
 	public class SubdirectoryEntry : DirectoryEntry {
+		protected uint directory_depth; // to detect recursive IFDs
+
 		public uint directory_offset;
 		public ImageDirectory [] Directory;
 		
-		public SubdirectoryEntry (byte [] data, int offset, Endian endian) : base (data, offset, endian)
+		public SubdirectoryEntry (byte [] data, int offset, Endian endian, uint depth) 
+			: base (data, offset, endian)
 		{
+			directory_depth = depth;
+
 			if (this.GetEntryCount () > 1) {
 				System.Console.WriteLine ("Count is greater than 1 ({1}) on Subdirectory {0} interesting", tagid, count);
 			}
@@ -1504,9 +1512,10 @@
 			for (int i = 0; i <  entry_count; i++) {
 				try {
 					directory_offset = BitConverter.ToUInt32 (raw_data, i * 4, endian == Endian.Little);
-					if (directory_offset == offset_origin + 8)
-						throw new Exception ("recursive ifd");
-					Directory [i] = new ImageDirectory (stream, directory_offset, endian);
+					if (directory_offset == offset_origin + 8
+					    || directory_depth > 4)
+						throw new Exception ("Recursive IFD (corrupt EXIF header)");
+					Directory [i] = new ImageDirectory (stream, directory_offset, endian, directory_depth + 1);
 				} catch (System.Exception e) {
 					System.Console.WriteLine ("Error loading Subdirectory {0} at {2} of {3}bytes:{4}{1}", 
 								  this.Id, e, directory_offset, stream.Length, Environment.NewLine);
