diff --git a/src/db/PhotoTable.vala b/src/db/PhotoTable.vala
index 70883fc..295dc49 100644
--- a/src/db/PhotoTable.vala
+++ b/src/db/PhotoTable.vala
@@ -332,7 +332,22 @@ public class PhotoTable : DatabaseTable {
         
         return true;
     }
-    
+
+    // Force corrupted orientations to a safe value.  This kind of 
+    // corruption could occur in previous versions of Shotwell that were
+    // compiled against certain versions of Valac that incorrectly handled
+    // objects passed as 'ref' arguments.  For further details, please see
+    // http://redmine.yorba.org/issues/4354 and 
+    // https://bugzilla.gnome.org/show_bug.cgi?id=663818 .
+    private void validate_orientation(PhotoRow row) {
+        if ((row.orientation < Orientation.MIN) ||
+            (row.orientation > Orientation.MAX)) {
+            // orientation was corrupted; set it to top left.
+            set_orientation(row.photo_id, Orientation.MIN);
+            row.orientation = Orientation.MIN;
+        }
+    }
+        
     public PhotoRow? get_row(PhotoID photo_id) {
         Sqlite.Statement stmt;
         int res = db.prepare_v2(
@@ -428,6 +443,8 @@ public class PhotoTable : DatabaseTable {
             row.development_ids[RawDeveloper.CAMERA] = BackingPhotoID(stmt.column_int64(26));
             row.development_ids[RawDeveloper.EMBEDDED] = BackingPhotoID(stmt.column_int64(27));
             
+            validate_orientation(row);
+            
             all.add(row);
         }
         
