--- IFDReader.cs.orig	2011-01-05 16:35:40.463754020 +0000
+++ IFDReader.cs	2011-01-05 20:25:16.599753999 +0000
@@ -181,7 +181,7 @@
 						break;
 					}
 					next_offset = ReadIFD (base_offset, next_offset, max_offset);
-				} while (next_offset > 0 && (count == -1 || ++i < count));
+				} while (next_offset > 0 && next_offset < max_offset && (count == -1 || ++i < count));
 
 				StopIFDLoopDetect ();
 			}
@@ -891,15 +891,20 @@
 				return new MakernoteIFDEntry (tag, ifd_structure, MakernoteType.Leica, header.Mid (0, 8), 10, false, null);
 			}
 
-			try {
-				IFDReader reader =
-					new IFDReader (file, is_bigendian, ifd_structure, base_offset, offset, max_offset);
+			// Only try and read if the data seems plausible
+			if ((2 + (header.Mid (0, 2).ToUShort (is_bigendian)) * 12) < count) {
+				try {
+					IFDReader reader =
+						new IFDReader (file, is_bigendian, ifd_structure, base_offset, offset, max_offset);
 
-				reader.Read ();
-				return new MakernoteIFDEntry (tag, ifd_structure, MakernoteType.Canon);
-			} catch {
-				return null;
+					reader.Read ();
+					return new MakernoteIFDEntry (tag, ifd_structure, MakernoteType.Canon);
+				} catch {
+					return null;
+				}
 			}
+
+			return null;
 		}
 
 #endregion
